0
Leetcode Problem 398. Random Pick Index
398. Random Pick Index
AI Mock Interview
Leetcode Solutions
Reservoir Sampling
Solution Idea
Algorithm Steps
Code Implementation
Complexity Analysis
Initialize a variable
count
to 0 to keep track of the number of times the target has been seen.
Iterate through the array
nums
.
For each index
i
, check if
nums[i]
is equal to the target.
If it is, increment
count
.
Generate a random number between 1 and
count
(inclusive).
If the random number is 1, select the current index
i
as the potential result.
Continue iterating through the array.
After the loop, return the last selected index.
Caching Results Using a Hashmap
Ask Question
Programming Language
Purpose:
General Question
Debug My Code
image/screenshot of info
(optional)
[+]
Full Screen
Loading...
Get Answer
Suggested Answer
Answer
Full Screen
Copy Answer Code
Loading...
Sign in with LinkedIn
Sign in with Github
OR
Sign in with Email link