0
Leetcode Problem 1157. Online Majority Element In Subarray
1157. Online Majority Element In Subarray
AI Mock Interview
Leetcode Solutions
Random Pick
Solution Idea
Algorithm Steps
Code Implementation
Complexity Analysis
Initialize a hashmap to store the indices of each element in the array.
For each query, randomly pick an element from the subarray specified by
left
and
right
.
Use binary search on the list of indices for the picked element to count its occurrences in the subarray.
If the count is greater than or equal to
threshold
, return the element as the majority.
Repeat steps 2-4 for a fixed number of times (e.g., 20 times).
If no majority element is found after all attempts, return -1.
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...