![]() |
VOOZH | about |
Given an array called arr[] and another array queries[]. You can perform an operation on arr at most once, replacing it with a subsequence of itself. Then, you process the queries in order. If the first and last elements of arr are both less than a query value queries[i], the processing ends. Otherwise, you can remove either the first or the last element of arr if it is greater than or equal to the query value. The task is to find the maximum number of queries that can be processed optimally.
Example:
Input: arr = {1,2,3,4,5}, queries = {1,2,3,4,6}
Output: 4
Explanation: The process for the queries is as follows:
- We choose and remove arr[0] since 1 <= 1, then arr becomes [2,3,4,5].
- We choose and remove arr[0] since 2 <= 2, then arr becomes [3,4,5].
- We choose and remove arr[0] since 3 <= 3, then arr becomes [4,5].
- We choose and remove arr[0] since 4 <= 4, then arr becomes [5].
- We cannot choose any elements from arr since they are not greater than or equal to 5. Hence, the answer is 4. It can be shown that we can't process more than 4 queries.
Input: arr = {2,3,2}, queries = {2,2,3}
Output: 3
Explanation: The process for the queries is as follows:
- We choose and remove arr[0] since 2 <= 2, then arr becomes {3,2}.
- We choose and remove arr[1] since 2 <= 2, then arr becomes {3}.
- We choose and remove arr[0] since 3 <= 3, then arr becomes []. Hence, the answer is 3. It can be shown that we can't process more than 3 queries.
Approach:
First, if we can do x operations, we can definitely do x−1 operations as well, which means we can use binary search to find the maximum operations.
Second, think of this problem: Given two arrays A and B with the same size. Initially, we have an empty array C, and we iterate each element in B. For each element, we can either put it in the leftmost of C, or put it in the rightmost of C. After all the operations, can we make A equal to C?
Solution: We can use DP to solve this problem. Let's say if dp[l][r] is true, then [l,r] in A can be obtained by the prefix of B with length r-l+1. For transition, dp[l][r] can be determined by dp[l+1][r] or dp[l][r-1]:
- if dp[l+1][r] is true, and B[r-l]==A[l],
- or dp[l][r-1] is true, and B[r-l]==A[r],
Can we convert the original problem to this one?
Actually, yes! But we need to reverse the queries, and choose a suffix as array B.
Also, we define dp[l][r] as the longest subsequence that can be obtained by [l,r] in arr, because in the original problem, we should choose a subsequence first. Then, as for transition, dp[l][r] can be determined by dp[l+1][r] or dp[l][r-1]: dp[l][r]=max(dp[l+1][r]+(a[l]>=queries[dp[l+1][r]]), dp[l][r-1]+(a[r]>=queries[dp[l][r-1]])).
If any dp[l][r] equals to the length of the suffix, we can get a subsquence of arr from the suffix.
Finally, we can use binary search to find the length of suffix of B, and the longest length is the answer.
Step-by-step approach:
Below is the implementation of the above approach:
4
Time complexity: O(n2log(n))
Auxiliary Space: O(n2log(n))