|
| 1 | +# [3018. Maximum Number of Removal Queries That Can Be Processed I](https://leetcode.cn/problems/maximum-number-of-removal-queries-that-can-be-processed-i) |
| 2 | + |
| 3 | +[English Version](/solution/3000-3099/3018.Maximum%20Number%20of%20Removal%20Queries%20That%20Can%20Be%20Processed%20I/README_EN.md) |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +<!-- 这里写题目描述 --> |
| 8 | + |
| 9 | +<p>You are given a <strong>0-indexed</strong> array <code>nums</code> and a <strong>0-indexed</strong> array <code>queries</code>.</p> |
| 10 | + |
| 11 | +<p>You can do the following operation at the beginning <strong>at most once</strong>:</p> |
| 12 | + |
| 13 | +<ul> |
| 14 | + <li>Replace <code>nums</code> with a <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</li> |
| 15 | +</ul> |
| 16 | + |
| 17 | +<p>We start processing queries in the given order; for each query, we do the following:</p> |
| 18 | + |
| 19 | +<ul> |
| 20 | + <li>If the first <strong>and</strong> the last element of <code>nums</code> is <strong>less than</strong> <code>queries[i]</code>, the processing of queries <strong>ends</strong>.</li> |
| 21 | + <li>Otherwise, we choose either the first <strong>or</strong> the last element of <code>nums</code> if it is <strong>greater than or equal to</strong> <code>queries[i]</code>, and we <strong>remove</strong> the chosen element from <code>nums</code>.</li> |
| 22 | +</ul> |
| 23 | + |
| 24 | +<p>Return <em>the <strong>maximum</strong> number of queries that can be processed by doing the operation optimally.</em></p> |
| 25 | + |
| 26 | +<p> </p> |
| 27 | +<p><strong class="example">Example 1:</strong></p> |
| 28 | + |
| 29 | +<pre> |
| 30 | +<strong>Input:</strong> nums = [1,2,3,4,5], queries = [1,2,3,4,6] |
| 31 | +<strong>Output:</strong> 4 |
| 32 | +<strong>Explanation:</strong> We don't do any operation and process the queries as follows: |
| 33 | +1- We choose and remove nums[0] since 1 <= 1, then nums becomes [2,3,4,5]. |
| 34 | +2- We choose and remove nums[0] since 2 <= 2, then nums becomes [3,4,5]. |
| 35 | +3- We choose and remove nums[0] since 3 <= 3, then nums becomes [4,5]. |
| 36 | +4- We choose and remove nums[0] since 4 <= 4, then nums becomes [5]. |
| 37 | +5- We can not choose any elements from nums since they are not greater than or equal to 5. |
| 38 | +Hence, the answer is 4. |
| 39 | +It can be shown that we can't process more than 4 queries. |
| 40 | +</pre> |
| 41 | + |
| 42 | +<p><strong class="example">Example 2:</strong></p> |
| 43 | + |
| 44 | +<pre> |
| 45 | +<strong>Input:</strong> nums = [2,3,2], queries = [2,2,3] |
| 46 | +<strong>Output:</strong> 3 |
| 47 | +<strong>Explanation:</strong> We don't do any operation and process the queries as follows: |
| 48 | +1- We choose and remove nums[0] since 2 <= 2, then nums becomes [3,2]. |
| 49 | +2- We choose and remove nums[1] since 2 <= 2, then nums becomes [3]. |
| 50 | +3- We choose and remove nums[0] since 3 <= 3, then nums becomes []. |
| 51 | +Hence, the answer is 3. |
| 52 | +It can be shown that we can't process more than 3 queries. |
| 53 | +</pre> |
| 54 | + |
| 55 | +<p><strong class="example">Example 3:</strong></p> |
| 56 | + |
| 57 | +<pre> |
| 58 | +<strong>Input:</strong> nums = [3,4,3], queries = [4,3,2] |
| 59 | +<strong>Output:</strong> 2 |
| 60 | +<strong>Explanation:</strong> First we replace nums with the subsequence of nums [4,3]. |
| 61 | +Then we can process the queries as follows: |
| 62 | +1- We choose and remove nums[0] since 4 <= 4, then nums becomes [3]. |
| 63 | +2- We choose and remove nums[0] since 3 <= 3, then nums becomes []. |
| 64 | +3- We can not process any more queries since nums is empty. |
| 65 | +Hence, the answer is 2. |
| 66 | +It can be shown that we can't process more than 2 queries. |
| 67 | +</pre> |
| 68 | + |
| 69 | +<p> </p> |
| 70 | +<p><strong>Constraints:</strong></p> |
| 71 | + |
| 72 | +<ul> |
| 73 | + <li><code>1 <= nums.length <= 1000</code></li> |
| 74 | + <li><code>1 <= queries.length <= 1000</code></li> |
| 75 | + <li><code>1 <= nums[i], queries[i] <= 10<sup>9</sup></code></li> |
| 76 | +</ul> |
| 77 | + |
| 78 | +## 解法 |
| 79 | + |
| 80 | +### 方法一 |
| 81 | + |
| 82 | +<!-- tabs:start --> |
| 83 | + |
| 84 | +```python |
| 85 | + |
| 86 | +``` |
| 87 | + |
| 88 | +```java |
| 89 | + |
| 90 | +``` |
| 91 | + |
| 92 | +```cpp |
| 93 | + |
| 94 | +``` |
| 95 | + |
| 96 | +```go |
| 97 | + |
| 98 | +``` |
| 99 | + |
| 100 | +<!-- tabs:end --> |
| 101 | + |
| 102 | +<!-- end --> |
0 commit comments