clueless coding // TODO: be smarter

LeetCode Random Pick Index: CPP Solution



TL;DR Code Solution



vector<int> data;

Solution(vector<int> nums) {
    this->data = nums;
}

int pick(int target) {
    int resovoir = 1;
    int index = -1;
    for (int i = 0; i < data.size(); i++) {
         if (data[i] == target) {
             if (rand() % resovoir == 0) {
                 index = i;
             }
             resovoir++;
         }
    }
    return index;
}