Problem solution · Python

Threshold Majority Queries

Threshold Majority Queries: a Python solution using sliding window or two pointers. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Sliding window or two pointers
Source
Kamyu LeetCode Solutions
Length
129 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Sliding window or two pointers

For Threshold Majority Queries, the implementation maintains a moving interval and updates only the information that enters or leaves the window.

  1. Choose the invariant that makes a window valid or useful.
  2. Advance the right boundary and add the new element.
  3. Move the left boundary only as needed while maintaining the invariant and updating the answer.

Code notes

  • 129 lines of Python from the credited upstream file threshold-majority-queries.py.
  • The implementation visibly relies on sequence storage, ordered lookup.
  • No explicit loop blocks detected.

Complexity

Confirm that neither pointer moves backwards; if so, the scan is usually linear apart from the window’s data-structure operations.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeThreshold Majority Queries · PythonPython
Use this to learn the idea, then write your own version.
# Time:  O(nlogn + qlogq + (n + q) * sqrt(n) + q * n)# Space: O(n + q) # sort, coordinate compression, mo's algorithmclass Solution(object):    def subarrayMajority(self, nums, queries):        """        :type nums: List[int]        :type queries: List[List[int]]        :rtype: List[int]        """        # reference: https://cp-algorithms.com/data_structures/sqrt_decomposition.html        def mo_s_algorithm():  # Time: O(QlogQ + (N + Q) * sqrt(N) + Q * N)            def add(i):  # Time: O(F) = O(1)                idx = num_to_idx[nums[i]]                if cnt[idx]:                    cnt2[cnt[idx]] -= 1                cnt[idx] += 1                cnt2[cnt[idx]] += 1                max_freq[0] = max(max_freq[0], cnt[idx])             def remove(i):  # Time: O(F) = O(1)                idx = num_to_idx[nums[i]]                cnt2[cnt[idx]] -= 1                if not cnt2[max_freq[0]]:                    max_freq[0] -= 1                cnt[idx] -= 1                if cnt[idx]:                    cnt2[cnt[idx]] += 1             def get_ans(t):  # Time: O(A) = O(N)                if max_freq[0] < t:                    return -1                i = next(i for i in xrange(len(cnt)) if cnt[i] == max_freq[0])                return sorted_nums[i]             cnt = [0]*len(num_to_idx)            cnt2 = [0]*(len(nums)+1)            max_freq = [0]            result = [-1]*len(queries)            block_size = int(len(nums)**0.5)+1  # O(S) = O(sqrt(N))            idxs = range(len(queries))            idxs.sort(key=lambda x: (queries[x][0]//block_size, queries[x][1] if (queries[x][0]//block_size)&1 else -queries[x][1]))  # Time: O(QlogQ)            left, right = 0, -1            for i in idxs:  # Time: O((N / S) * N * F + S * Q * F + Q * A) = O((N + Q) * sqrt(N) + Q * N), O(S) = O(sqrt(N)), O(F) = O(logN), O(A) = O(1)                l, r, t = queries[i]                while left > l:                    left -= 1                    add(left)                while right < r:                    right += 1                    add(right)                while left < l:                    remove(left)                    left += 1                while right > r:                    remove(right)                    right -= 1                result[i] = get_ans(t)            return result         sorted_nums = sorted(set(nums))        num_to_idx = {x:i for i, x in enumerate(sorted_nums)}        return mo_s_algorithm()  # Time:  O(nlogn + qlogq + (n + q) * sqrt(n) * logn)# Space: O(n + q)from sortedcontainers import SortedList  # sort, coordinate compression, mo's algorithm, sorted listclass Solution_TLE(object):    def subarrayMajority(self, nums, queries):        """        :type nums: List[int]        :type queries: List[List[int]]        :rtype: List[int]        """        # reference: https://cp-algorithms.com/data_structures/sqrt_decomposition.html        def mo_s_algorithm():  # Time: O(QlogQ + (N + Q) * sqrt(N) * logN)            def add(i):  # Time: O(F) = O(logN)                idx = num_to_idx[nums[i]]                if cnt[idx]:                    lookup[cnt[idx]].remove(nums[i])                cnt[idx] += 1                lookup[cnt[idx]].add(nums[i])                max_freq[0] = max(max_freq[0], cnt[idx])             def remove(i):  # Time: O(F) = O(logN)                idx = num_to_idx[nums[i]]                lookup[cnt[idx]].remove(nums[i])                if not lookup[max_freq[0]]:                    max_freq[0] -= 1                cnt[idx] -= 1                if cnt[idx]:                    lookup[cnt[idx]].add(nums[i])             def get_ans(t):  # Time: O(A) = O(logN)                return lookup[max_freq[0]][0] if max_freq[0] >= t else -1             cnt = [0]*len(num_to_idx)            lookup = [SortedList() for _ in xrange(len(nums)+1)]            max_freq = [0]            result = [-1]*len(queries)            block_size = int(len(nums)**0.5)+1  # O(S) = O(sqrt(N))            idxs = range(len(queries))            idxs.sort(key=lambda x: (queries[x][0]//block_size, queries[x][1] if (queries[x][0]//block_size)&1 else -queries[x][1]))  # Time: O(QlogQ)            left, right = 0, -1            for i in idxs:  # Time: O((N / S) * N * F + S * Q * F + Q * A) = O((N + Q) * sqrt(N) * logN), O(S) = O(sqrt(N)), O(F) = O(logN), O(A) = O(1)                l, r, t = queries[i]                while left > l:                    left -= 1                    add(left)                while right < r:                    right += 1                    add(right)                while left < l:                    remove(left)                    left += 1                while right > r:                    remove(right)                    right -= 1                result[i] = get_ans(t)            return result         num_to_idx = {x:i for i, x in enumerate(sorted(set(nums)))}        return mo_s_algorithm() 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗