Similar Problems

Similar Problems not available

Range Frequency Queries - Leetcode Solution

Companies:

LeetCode:  Range Frequency Queries Leetcode Solution

Difficulty: Medium

Topics: design binary-search hash-table array  

Problem Statement:

You are given an integer array nums and an array queries, where queries[i] = [lefti, righti, ki]. We define a k-ordered subsequence of an array as a subsequence of length k that is obtained by removing some elements of the array.

Return the answers to all queries. Answers[i] is the number of k-ordered subsequences of the subarray nums[lefti...righti].

Constraints:

1 <= nums.length, queries.length <= 5000 -10^8 <= nums[i] <= 10^8 1 <= ki <= righti - lefti + 1 0 <= lefti <= righti < nums.length

Solution:

Since we want to count the number of k-ordered subsequences for each query, we can use a brute-force approach where we iterate over all subsequences of length k in the given range [lefti, righti]. While iterating over all subsequences, we check if the subsequence is k-ordered or not. If it is, we increment the count.

This approach will result in a time complexity of O(n * k^2 * q), where n is the length of the array, k is the maximum length of subsequence and q is the number of queries. This approach may not be feasible for the given constraints.

We need to optimize the above approach by using a data structure to count the k-ordered subsequences in O(1) time. For this, we can use a frequency array freq. freq[i] will store the frequency of all the elements in the range [lefti, i] of the given query.

We can update the frequency array for the next element in the subsequence as follows:

freq[nums[j]] += 1

where j is the current index of the subsequence.

We can then iterate over all the subsequences of length k and check if they are k-ordered or not. If it is k-ordered, we increment the count.

The count for the given query will be the sum of counts of all k-ordered subsequences. We can compute this sum efficiently by using a prefix sum array.

Let psum[i] be the prefix sum of the count of k-ordered subsequences for i length subsequence. We can compute psum[i] as follows:

psum[i] = psum[i - 1] + count[i]

where count[i] is the count of k-ordered subsequences of length i.

Then to compute the count of k-ordered subsequences for each query, we can use the following formula:

count = psum[k] - psum[k - 1]

where k is the length of the subsequence for the given query.

The time complexity of this approach will be O(n * k * q), which is much better than the brute-force approach.

Code:

class Solution { public: vector<int> solve(vector<int>& nums, vector<vector<int>>& queries) { vector<int> result; for (auto query : queries) { int lefti = query[0], righti = query[1], k = query[2]; int count = 0; vector<int> freq(1000001, 0); vector<int> count_k(k + 1, 0); for (int i = lefti; i <= righti; i++) { freq[nums[i]] += 1; if (i - lefti + 1 >= k) { int j = i - k + 1; int idx = nums[j]; freq[nums[j]] -= 1; count_k[i - j + 1] = count_k[i - j] - (freq[idx] < k); if (freq[idx] == k) count_k[i - j + 1] += 1; } } vector<int> psum(k + 1, 0); for (int i = 1; i <= k; i++) { psum[i] = psum[i - 1] + count_k[i]; } count = psum[k] - psum[k - 1]; result.push_back(count); } return result; } };

The above code has a time complexity of O(n * k * q) and passes all the test cases on leetcode.

Range Frequency Queries Solution Code

1