Maximum Number of Removal Queries That Can Be Processed I Problem
Description
LeetCode Problem 3018.
You are given a 0-indexed array nums and a 0-indexed array queries.
You can do the following operation at the beginning at most once:
- Replace
numswith a subsequence ofnums.
We start processing queries in the given order; for each query, we do the following:
- If the first and the last element of
numsis less thanqueries[i], the processing of queries ends. - Otherwise, we choose either the first or the last element of
numsif it is greater than or equal toqueries[i], and we remove the chosen element fromnums.
Return the maximum number of queries that can be processed by doing the operation optimally.
Example 1:
Input: nums = [1,2,3,4,5], queries = [1,2,3,4,6]
Output: 4
Explanation: We don't do any operation and process the queries as follows:
1- We choose and remove nums[0] since 1 <= 1, then nums becomes [2,3,4,5].
2- We choose and remove nums[0] since 2 <= 2, then nums becomes [3,4,5].
3- We choose and remove nums[0] since 3 <= 3, then nums becomes [4,5].
4- We choose and remove nums[0] since 4 <= 4, then nums becomes [5].
5- We can not choose any elements from nums 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.
Example 2:
Input: nums = [2,3,2], queries = [2,2,3]
Output: 3
Explanation: We don't do any operation and process the queries as follows:
1- We choose and remove nums[0] since 2 <= 2, then nums becomes [3,2].
2- We choose and remove nums[1] since 2 <= 2, then nums becomes [3].
3- We choose and remove nums[0] since 3 <= 3, then nums becomes [].
Hence, the answer is 3.
It can be shown that we can't process more than 3 queries.
Example 3:
Input: nums = [3,4,3], queries = [4,3,2]
Output: 2
Explanation: First we replace nums with the subsequence of nums [4,3].
Then we can process the queries as follows:
1- We choose and remove nums[0] since 4 <= 4, then nums becomes [3].
2- We choose and remove nums[0] since 3 <= 3, then nums becomes [].
3- We can not process any more queries since nums is empty.
Hence, the answer is 2.
It can be shown that we can't process more than 2 queries.
Constraints:
1 <= nums.length <= 10001 <= queries.length <= 10001 <= nums[i], queries[i] <= 10^9
Solution
Approach: Interval Dynamic Programming
Every query removes an element from either end of nums, so after some queries the remaining elements form an interval. Let f[i][j] be the largest number of queries already processed when exactly nums[i..j] is still left (so the query to process next is queries[f[i][j]]).
- Reach
[i, j]from[i - 1, j]by removingnums[i - 1]; this is allowed ifnums[i - 1] >= queries[f[i - 1][j]], which processes one more query. - Reach it from
[i, j + 1]by removingnums[j + 1]under the same condition.
Take the best of the two (or keep the count if neither removal is allowed). If some state processes all m queries, return m. Otherwise the last removal happens when one element is left, so the answer is the maximum over i of f[i][i] + (nums[i] >= queries[f[i][i]]).
Complexity: O(n2) time and O(n2) space.
class Solution:
def maximumProcessableQueries(self, nums: List[int], queries: List[int]) -> int:
n = len(nums)
f = [[0] * n for _ in range(n)]
m = len(queries)
for i in range(n):
for j in range(n - 1, i - 1, -1):
if i:
f[i][j] = max(
f[i][j], f[i - 1][j] + (nums[i - 1] >= queries[f[i - 1][j]])
)
if j + 1 < n:
f[i][j] = max(
f[i][j], f[i][j + 1] + (nums[j + 1] >= queries[f[i][j + 1]])
)
if f[i][j] == m:
return m
return max(f[i][i] + (nums[i] >= queries[f[i][i]]) for i in range(n))