/*
* 算法思想:
* 蓄水池抽样算法,遍历整个数组,根据蓄水池抽样算法,决定当前元素的舍弃或者保留。
*
*/
typedef struct {
int *arr;
int len;
} Solution;
Solution* solutionCreate(int* nums, int numsSize) {
int i;
Solution* obj = (Solution*)malloc(sizeof(Solution));
obj->arr = nums;
obj->len = numsSize;
return obj;
}
int solutionPick(Solution* obj, int target) {
int i=0;
int ret = INT_MIN;
int cnt = 0;
for(i=0; i<obj->len; i++){
if(obj->arr[i] == target){
cnt++;
ret = (rand()%cnt == 0) ? i : ret;
}
}
return ret;
}
void solutionFree(Solution* obj) {
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);
*/