LeetCode //C - 398. Random Pick Index

398. Random Pick Index

Given an integer array nums with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array.

Implement the Solution class:

  • Solution(int[] nums) Initializes the object with the array nums.
  • int pick(int target) Picks a random index i from nums where nums[i] == target. If there are multiple valid i’s, then each index should have an equal probability of returning.
     
Example 1:

Input:
[“Solution”, “pick”, “pick”, “pick”]
[[[1, 2, 3, 3, 3]], [3], [1], [3]]
Output:
[null, 4, 0, 2]
Explanation:
Solution solution = new Solution([1, 2, 3, 3, 3]);
solution.pick(3); // It should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning.
solution.pick(1); // It should return 0. Since in the array only nums[0] is equal to 1.
solution.pick(3); // It should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning.

Constraints:
  • 1 < = n u m s . l e n g t h < = 2 ∗ 1 0 4 1 <= nums.length <= 2 * 10^4 1<=nums.length<=2104
  • − 2 31 < = n u m s [ i ] < = 2 31 − 1 -2^{31} <= nums[i] <= 2^{31} - 1 231<=nums[i]<=2311
  • target is an integer from nums.
  • At most 1 0 4 10^4 104 calls will be made to pick.

From: LeetCode
Link: 398. Random Pick Index


Solution:

Ideas:

1. Struct Definition:

  • Solution struct holds:
    • nums: a pointer to the input array.
    • numsSize: the size of the array.

2. solutionCreate:

  • This function initializes the Solution object with the given nums array and stores its size in numsSize.

3. solutionPick:

  • We use reservoir sampling to randomly select an index of target:
    • We iterate through the nums array.
    • For each occurrence of target, we increment the count of valid indices.
    • Using the formula rand() % count == 0, we decide whether to select the current index as the result. This ensures that each index has an equal probability of being selected.

4. solutionFree:

  • This function frees the dynamically allocated memory for the Solution object.
Code:
typedef struct {
    int* nums;    // Array to store the nums
    int numsSize; // Size of the array
} Solution;

Solution* solutionCreate(int* nums, int numsSize) {
    Solution* obj = (Solution*)malloc(sizeof(Solution));
    obj->nums = nums;
    obj->numsSize = numsSize;
    return obj;
}

int solutionPick(Solution* obj, int target) {
    int count = 0;  // To count the number of occurrences of target
    int result = -1; // Store the result index

    // Iterate over the nums array
    for (int i = 0; i < obj->numsSize; i++) {
        if (obj->nums[i] == target) {
            count++;
            // Reservoir sampling: replace result with probability 1/count
            if (rand() % count == 0) {
                result = i;
            }
        }
    }

    return result;
}

void solutionFree(Solution* obj) {
    // No need to free the nums array because it is passed in externally
    free(obj);
}

/**
 * Your Solution struct will be instantiated and called as such:
 * Solution* obj = solutionCreate(nums, numsSize);
 * int param_1 = solutionPick(obj, target);
 * solutionFree(obj);
 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Navigator_Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值