844. Maximum Number of Removal Queries That Can Be Processed I
You are given a 0-indexed integer array nums and a 0-indexed integer array queries.
You are allowed to perform the following operation at most once before processing the queries:
- Replace nums with any subsequence of nums.
After that, you process the queries in the given order. For each query, do the following:
- If both the first and the last elements of nums are strictly less than the current query value queries[i], then stop processing further queries.
- Otherwise, if at least one of the two elements (the first or the last) is greater than or equal to queries[i], choose either eligible element and remove it from nums.
Return the maximum number of queries that can be processed when you choose the operation optimally.
Example 1:
Input : nums = [5,6,7,8,9], queries = [5,7,8,10,6]
Output : 3
Explanation : Without performing any operation, process the queries as follows:
- Query 5: The first element is 5 and the last is 9. Since 5 ≥ 5, remove 5 (the first element). Now, nums = [6,7,8,9].
- Query 7: The first element is 6 (< 7) but the last element is 9 (≥ 7); remove 9 (the last element). Now, nums = [6,7,8].
- Query 8: The first element is 6 (< 8) but the last element is 8 (≥ 8); remove 8 (the last element). Now, nums = [6,7].
- Query 10: Both the first (6) and the last (7) are less than 10, so processing stops. Thus, 3 queries are processed.
Example 2:
Input : nums = [10,5,11], queries = [6,5,10]
Output : 3
Explanation : Without using the operation, process the queries as follows:
- Query 6: The first element is 10 (≥ 6); remove it. Now, nums = [5,11].
- Query 5: The first element is 5 (≥ 5); remove it. Now, nums = [11].
- Query 10: The remaining element is 11 (≥ 10); remove it. Now, nums = []. All 3 queries are successfully processed.
Now Your Turn!
Pick the correct output for the given inputInput : nums = [8,3,6], queries = [7,5,2]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= nums.length <= 1000
- 1 <= queries.length <= 1000
- 1 <= nums[i], queries[i] <= 109