|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: 困难 |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3231.Minimum%20Number%20of%20Increasing%20Subsequence%20to%20Be%20Removed/README.md |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- problem:start --> |
| 8 | + |
| 9 | +# [3231. Minimum Number of Increasing Subsequence to Be Removed 🔒](https://leetcode.cn/problems/minimum-number-of-increasing-subsequence-to-be-removed) |
| 10 | + |
| 11 | +[English Version](/solution/3200-3299/3231.Minimum%20Number%20of%20Increasing%20Subsequence%20to%20Be%20Removed/README_EN.md) |
| 12 | + |
| 13 | +## 题目描述 |
| 14 | + |
| 15 | +<!-- description:start --> |
| 16 | + |
| 17 | +<p>Given an array of integers <code>nums</code>, you are allowed to perform the following operation any number of times:</p> |
| 18 | + |
| 19 | +<ul> |
| 20 | + <li>Remove a <strong>strictly increasing</strong> <span data-keyword="subsequence-array">subsequence</span> from the array.</li> |
| 21 | +</ul> |
| 22 | + |
| 23 | +<p>Your task is to find the <strong>minimum</strong> number of operations required to make the array <strong>empty</strong>.</p> |
| 24 | + |
| 25 | +<p> </p> |
| 26 | +<p><strong class="example">Example 1:</strong></p> |
| 27 | + |
| 28 | +<div class="example-block"> |
| 29 | +<p><strong>Input:</strong> <span class="example-io">nums = [5,3,1,4,2]</span></p> |
| 30 | + |
| 31 | +<p><strong>Output:</strong> <span class="example-io">3</span></p> |
| 32 | + |
| 33 | +<p><strong>Explanation:</strong></p> |
| 34 | + |
| 35 | +<p>We remove subsequences <code>[1, 2]</code>, <code>[3, 4]</code>, <code>[5]</code>.</p> |
| 36 | +</div> |
| 37 | + |
| 38 | +<p><strong class="example">Example 2:</strong></p> |
| 39 | + |
| 40 | +<div class="example-block"> |
| 41 | +<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5]</span></p> |
| 42 | + |
| 43 | +<p><strong>Output:</strong> <span class="example-io">1</span></p> |
| 44 | +</div> |
| 45 | + |
| 46 | +<p><strong class="example">Example 3:</strong></p> |
| 47 | + |
| 48 | +<div class="example-block"> |
| 49 | +<p><strong>Input:</strong> <span class="example-io">nums = [5,4,3,2,1]</span></p> |
| 50 | + |
| 51 | +<p><strong>Output:</strong> <span class="example-io">5</span></p> |
| 52 | +</div> |
| 53 | + |
| 54 | +<p> </p> |
| 55 | +<p><strong>Constraints:</strong></p> |
| 56 | + |
| 57 | +<ul> |
| 58 | + <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> |
| 59 | + <li><code>1 <= nums[i] <= 10<sup>5</sup></code></li> |
| 60 | +</ul> |
| 61 | + |
| 62 | +<!-- description:end --> |
| 63 | + |
| 64 | +## 解法 |
| 65 | + |
| 66 | +<!-- solution:start --> |
| 67 | + |
| 68 | +### 方法一:贪心 + 二分查找 |
| 69 | + |
| 70 | +我们从左到右遍历数组 $\textit{nums}$,对于每个元素 $x$,我们需要贪心地将其追加到前面序列中最后一个元素小于 $x$ 的最大值后面。如果找不到这样的元素,则说明当前元素 $x$ 比前面序列中的所有元素都小,我们需要新开辟一个序列,将 $x$ 放入其中。 |
| 71 | + |
| 72 | +这样分析下来,我们可以发现,前面序列中的最后一个元素呈单调递减的状态。因此,我们可以使用二分查找来找到前面序列中最后一个元素小于 $x$ 的第一个元素位置,然后将 $x$ 放入该位置。 |
| 73 | + |
| 74 | +最终,我们返回序列的个数即可。 |
| 75 | + |
| 76 | +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。 |
| 77 | + |
| 78 | +<!-- tabs:start --> |
| 79 | + |
| 80 | +#### Python3 |
| 81 | + |
| 82 | +```python |
| 83 | +class Solution: |
| 84 | + def minOperations(self, nums: List[int]) -> int: |
| 85 | + g = [] |
| 86 | + for x in nums: |
| 87 | + l, r = 0, len(g) |
| 88 | + while l < r: |
| 89 | + mid = (l + r) >> 1 |
| 90 | + if g[mid] < x: |
| 91 | + r = mid |
| 92 | + else: |
| 93 | + l = mid + 1 |
| 94 | + if l == len(g): |
| 95 | + g.append(x) |
| 96 | + else: |
| 97 | + g[l] = x |
| 98 | + return len(g) |
| 99 | +``` |
| 100 | + |
| 101 | +#### Java |
| 102 | + |
| 103 | +```java |
| 104 | +class Solution { |
| 105 | + public int minOperations(int[] nums) { |
| 106 | + List<Integer> g = new ArrayList<>(); |
| 107 | + for (int x : nums) { |
| 108 | + int l = 0, r = g.size(); |
| 109 | + while (l < r) { |
| 110 | + int mid = (l + r) >> 1; |
| 111 | + if (g.get(mid) < x) { |
| 112 | + r = mid; |
| 113 | + } else { |
| 114 | + l = mid + 1; |
| 115 | + } |
| 116 | + } |
| 117 | + if (l == g.size()) { |
| 118 | + g.add(x); |
| 119 | + } else { |
| 120 | + g.set(l, x); |
| 121 | + } |
| 122 | + } |
| 123 | + return g.size(); |
| 124 | + } |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +#### C++ |
| 129 | + |
| 130 | +```cpp |
| 131 | +class Solution { |
| 132 | +public: |
| 133 | + int minOperations(vector<int>& nums) { |
| 134 | + vector<int> g; |
| 135 | + for (int x : nums) { |
| 136 | + int l = 0, r = g.size(); |
| 137 | + while (l < r) { |
| 138 | + int mid = (l + r) >> 1; |
| 139 | + if (g[mid] < x) { |
| 140 | + r = mid; |
| 141 | + } else { |
| 142 | + l = mid + 1; |
| 143 | + } |
| 144 | + } |
| 145 | + if (l == g.size()) { |
| 146 | + g.push_back(x); |
| 147 | + } else { |
| 148 | + g[l] = x; |
| 149 | + } |
| 150 | + } |
| 151 | + return g.size(); |
| 152 | + } |
| 153 | +}; |
| 154 | +``` |
| 155 | +
|
| 156 | +#### Go |
| 157 | +
|
| 158 | +```go |
| 159 | +func minOperations(nums []int) int { |
| 160 | + g := []int{} |
| 161 | + for _, x := range nums { |
| 162 | + l, r := 0, len(g) |
| 163 | + for l < r { |
| 164 | + mid := (l + r) >> 1 |
| 165 | + if g[mid] < x { |
| 166 | + r = mid |
| 167 | + } else { |
| 168 | + l = mid + 1 |
| 169 | + } |
| 170 | + } |
| 171 | + if l == len(g) { |
| 172 | + g = append(g, x) |
| 173 | + } else { |
| 174 | + g[l] = x |
| 175 | + } |
| 176 | + } |
| 177 | + return len(g) |
| 178 | +} |
| 179 | +``` |
| 180 | + |
| 181 | +#### TypeScript |
| 182 | + |
| 183 | +```ts |
| 184 | +function minOperations(nums: number[]): number { |
| 185 | + const g: number[] = []; |
| 186 | + for (const x of nums) { |
| 187 | + let [l, r] = [0, g.length]; |
| 188 | + while (l < r) { |
| 189 | + const mid = (l + r) >> 1; |
| 190 | + if (g[mid] < x) { |
| 191 | + r = mid; |
| 192 | + } else { |
| 193 | + l = mid + 1; |
| 194 | + } |
| 195 | + } |
| 196 | + if (l === g.length) { |
| 197 | + g.push(x); |
| 198 | + } else { |
| 199 | + g[l] = x; |
| 200 | + } |
| 201 | + } |
| 202 | + return g.length; |
| 203 | +} |
| 204 | +``` |
| 205 | + |
| 206 | +#### Rust |
| 207 | + |
| 208 | +```rust |
| 209 | +impl Solution { |
| 210 | + pub fn min_operations(nums: Vec<i32>) -> i32 { |
| 211 | + let mut g = Vec::new(); |
| 212 | + for &x in nums.iter() { |
| 213 | + let mut l = 0; |
| 214 | + let mut r = g.len(); |
| 215 | + while l < r { |
| 216 | + let mid = (l + r) / 2; |
| 217 | + if g[mid] < x { |
| 218 | + r = mid; |
| 219 | + } else { |
| 220 | + l = mid + 1; |
| 221 | + } |
| 222 | + } |
| 223 | + if l == g.len() { |
| 224 | + g.push(x); |
| 225 | + } else { |
| 226 | + g[l] = x; |
| 227 | + } |
| 228 | + } |
| 229 | + g.len() as i32 |
| 230 | + } |
| 231 | +} |
| 232 | +``` |
| 233 | + |
| 234 | +<!-- tabs:end --> |
| 235 | + |
| 236 | +<!-- solution:end --> |
| 237 | + |
| 238 | +<!-- problem:end --> |
0 commit comments