Threshold Majority Queries Problem
Description
LeetCode Problem 3636.
You are given an integer array nums of length n and an array queries, where queries[i] = [l_i, r_i, threshold_i].
Return an array of integers ans where ans[i] is equal to the element in the subarray nums[l_i...r_i] that appears at least threshold_i times, selecting the element with the highest frequency (choosing the smallest in case of a tie), or -1 if no such element exists.
Example 1:
Input: nums = [1,1,2,2,1,1], queries = [[0,5,4],[0,3,3],[2,3,2]]
Output: [1,-1,2]
Explanation:
Query Sub-array Threshold Frequency table Answer
[0, 5, 4] [1, 1, 2, 2, 1, 1] 4 1 → 4, 2 → 2 1
[0, 3, 3] [1, 1, 2, 2] 3 1 → 2, 2 → 2 -1
[2, 3, 2] [2, 2] 2 2 → 2 2
Example 2:
Input: nums = [3,2,3,2,3,2,3], queries = [[0,6,4],[1,5,2],[2,4,1],[3,3,1]]
Output: [3,2,3,2]
Explanation:
Query Sub-array Threshold Frequency table Answer
[0, 6, 4] [3, 2, 3, 2, 3, 2, 3] 4 3 → 4, 2 → 3 3
[1, 5, 2] [2, 3, 2, 3, 2] 2 2 → 3, 3 → 2 2
[2, 4, 1] [3, 2, 3] 1 3 → 2, 2 → 1 3
[3, 3, 1] [2] 1 2 → 1 2
Constraints:
1 <= nums.length == n <= 10^41 <= nums[i] <= 10^91 <= queries.length <= 5 * 10^4queries[i] = [l_i, r_i, threshold_i]0 <= l_i <= r_i < n1 <= threshold_i <= r_i - l_i + 1
Solution
Approach: Mo's Algorithm with Rollback
For a query we need the most frequent value in nums[l..r] (the smallest one on a tie); it is the answer if its frequency is at least the threshold, otherwise -1.
First compress the values to their ranks, so that a smaller rank means a smaller value. Then sort the queries by the block of l (block size about n / sqrt(q)) and by r. Inside one block:
- the right end only moves forward, adding elements to a frequency array and keeping the best pair (frequency, smallest value) so far; adding is easy to maintain, while removing would not be;
- for each query, add the elements from
lto the end of the block, read the best pair, and then undo exactly those additions; - a query that lies entirely inside the block is answered by brute force over its few elements and undone the same way.
At the end of the block, undo the elements added on the right.
Complexity: O((n + q) sqrt(n)) time and O(n) space, where n is the length of the array and q the number of queries.
import math
class Solution:
def subarrayMajority(self, nums: List[int], queries: List[List[int]]) -> List[int]:
n = len(nums)
vals = sorted(set(nums))
rank = {v: i for i, v in enumerate(vals)}
a = [rank[v] for v in nums]
size = max(1, int(n / math.sqrt(len(queries))))
order = sorted(range(len(queries)), key=lambda i: (queries[i][0] // size, queries[i][1]))
ans = [-1] * len(queries)
cnt = [0] * len(vals)
i = 0
while i < len(order):
blk = queries[order[i]][0] // size
end = (blk + 1) * size - 1 # last index of this block
j = i
while j < len(order) and queries[order[j]][0] // size == blk:
j += 1
r = end # the window [end + 1, r] only grows
bf, bv = 0, n # best frequency and its (smallest) value rank
for qi in order[i:j]:
l, rr, th = queries[qi]
if rr <= end:
f, v = 0, n
for x in range(l, rr + 1):
cnt[a[x]] += 1
c = cnt[a[x]]
if c > f or (c == f and a[x] < v):
f, v = c, a[x]
for x in range(l, rr + 1):
cnt[a[x]] -= 1
ans[qi] = vals[v] if f >= th else -1
continue
while r < rr:
r += 1
cnt[a[r]] += 1
c = cnt[a[r]]
if c > bf or (c == bf and a[r] < bv):
bf, bv = c, a[r]
f, v = bf, bv
for x in range(l, end + 1):
cnt[a[x]] += 1
c = cnt[a[x]]
if c > f or (c == f and a[x] < v):
f, v = c, a[x]
for x in range(l, end + 1):
cnt[a[x]] -= 1
ans[qi] = vals[v] if f >= th else -1
for x in range(end + 1, r + 1):
cnt[a[x]] -= 1
i = j
return ans