bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1157. Online Majority Element In Subarray

1157. Online Majority Element In Subarray

Leetcode Solutions

Random Pick

  1. Initialize a hashmap to store the indices of each element in the array.
  2. For each query, randomly pick an element from the subarray specified by left and right.
  3. Use binary search on the list of indices for the picked element to count its occurrences in the subarray.
  4. If the count is greater than or equal to threshold, return the element as the majority.
  5. Repeat steps 2-4 for a fixed number of times (e.g., 20 times).
  6. If no majority element is found after all attempts, return -1.
UML Thumbnail

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...