From 95fca1ef4ab7278d696af147d9dc73f5a71606a0 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Thu, 25 Jul 2024 08:46:31 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3231 No.3231.Minimum Number of Increasing Subsequence to Be Removed --- .../0545.Boundary of Binary Tree/README.md | 2 +- .../README.md | 238 ++++++++++++++++++ .../README_EN.md | 238 ++++++++++++++++++ .../Solution.cpp | 23 ++ .../Solution.go | 20 ++ .../Solution.java | 22 ++ .../Solution.py | 16 ++ .../Solution.rs | 23 ++ .../Solution.ts | 20 ++ solution/README.md | 1 + solution/README_EN.md | 1 + 11 files changed, 603 insertions(+), 1 deletion(-) create mode 100644 solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/README.md create mode 100644 solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/README_EN.md create mode 100644 solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/Solution.cpp create mode 100644 solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/Solution.go create mode 100644 solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/Solution.java create mode 100644 solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/Solution.py create mode 100644 solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/Solution.rs create mode 100644 solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/Solution.ts diff --git a/solution/0500-0599/0545.Boundary of Binary Tree/README.md b/solution/0500-0599/0545.Boundary of Binary Tree/README.md index 0c644aea6dee6..57937625d0e11 100644 --- a/solution/0500-0599/0545.Boundary of Binary Tree/README.md +++ b/solution/0500-0599/0545.Boundary of Binary Tree/README.md @@ -18,7 +18,7 @@ tags: -

二叉树的 边界 是由 根节点 , 左边界 , 按从左到右顺序的 叶节点逆序的右边界 ,按顺序依次连接组成。

+

二叉树的 边界 是由 根节点 左边界 、按从左到右顺序的 叶节点逆序的右边界 ,按顺序依次连接组成。

左边界 是满足下述定义的节点集合:

diff --git a/solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/README.md b/solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/README.md new file mode 100644 index 0000000000000..fb6ed09a68c9a --- /dev/null +++ b/solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/README.md @@ -0,0 +1,238 @@ +--- +comments: true +difficulty: 困难 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3231.Minimum%20Number%20of%20Increasing%20Subsequence%20to%20Be%20Removed/README.md +--- + + + +# [3231. Minimum Number of Increasing Subsequence to Be Removed 🔒](https://leetcode.cn/problems/minimum-number-of-increasing-subsequence-to-be-removed) + +[English Version](/solution/3200-3299/3231.Minimum%20Number%20of%20Increasing%20Subsequence%20to%20Be%20Removed/README_EN.md) + +## 题目描述 + + + +

Given an array of integers nums, you are allowed to perform the following operation any number of times:

+ + + +

Your task is to find the minimum number of operations required to make the array empty.

+ +

 

+

Example 1:

+ +
+

Input: nums = [5,3,1,4,2]

+ +

Output: 3

+ +

Explanation:

+ +

We remove subsequences [1, 2], [3, 4], [5].

+
+ +

Example 2:

+ +
+

Input: nums = [1,2,3,4,5]

+ +

Output: 1

+
+ +

Example 3:

+ +
+

Input: nums = [5,4,3,2,1]

+ +

Output: 5

+
+ +

 

+

Constraints:

+ + + + + +## 解法 + + + +### 方法一:贪心 + 二分查找 + +我们从左到右遍历数组 $\textit{nums}$,对于每个元素 $x$,我们需要贪心地将其追加到前面序列中最后一个元素小于 $x$ 的最大值后面。如果找不到这样的元素,则说明当前元素 $x$ 比前面序列中的所有元素都小,我们需要新开辟一个序列,将 $x$ 放入其中。 + +这样分析下来,我们可以发现,前面序列中的最后一个元素呈单调递减的状态。因此,我们可以使用二分查找来找到前面序列中最后一个元素小于 $x$ 的第一个元素位置,然后将 $x$ 放入该位置。 + +最终,我们返回序列的个数即可。 + +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。 + + + +#### Python3 + +```python +class Solution: + def minOperations(self, nums: List[int]) -> int: + g = [] + for x in nums: + l, r = 0, len(g) + while l < r: + mid = (l + r) >> 1 + if g[mid] < x: + r = mid + else: + l = mid + 1 + if l == len(g): + g.append(x) + else: + g[l] = x + return len(g) +``` + +#### Java + +```java +class Solution { + public int minOperations(int[] nums) { + List g = new ArrayList<>(); + for (int x : nums) { + int l = 0, r = g.size(); + while (l < r) { + int mid = (l + r) >> 1; + if (g.get(mid) < x) { + r = mid; + } else { + l = mid + 1; + } + } + if (l == g.size()) { + g.add(x); + } else { + g.set(l, x); + } + } + return g.size(); + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int minOperations(vector& nums) { + vector g; + for (int x : nums) { + int l = 0, r = g.size(); + while (l < r) { + int mid = (l + r) >> 1; + if (g[mid] < x) { + r = mid; + } else { + l = mid + 1; + } + } + if (l == g.size()) { + g.push_back(x); + } else { + g[l] = x; + } + } + return g.size(); + } +}; +``` + +#### Go + +```go +func minOperations(nums []int) int { + g := []int{} + for _, x := range nums { + l, r := 0, len(g) + for l < r { + mid := (l + r) >> 1 + if g[mid] < x { + r = mid + } else { + l = mid + 1 + } + } + if l == len(g) { + g = append(g, x) + } else { + g[l] = x + } + } + return len(g) +} +``` + +#### TypeScript + +```ts +function minOperations(nums: number[]): number { + const g: number[] = []; + for (const x of nums) { + let [l, r] = [0, g.length]; + while (l < r) { + const mid = (l + r) >> 1; + if (g[mid] < x) { + r = mid; + } else { + l = mid + 1; + } + } + if (l === g.length) { + g.push(x); + } else { + g[l] = x; + } + } + return g.length; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn min_operations(nums: Vec) -> i32 { + let mut g = Vec::new(); + for &x in nums.iter() { + let mut l = 0; + let mut r = g.len(); + while l < r { + let mid = (l + r) / 2; + if g[mid] < x { + r = mid; + } else { + l = mid + 1; + } + } + if l == g.len() { + g.push(x); + } else { + g[l] = x; + } + } + g.len() as i32 + } +} +``` + + + + + + diff --git a/solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/README_EN.md b/solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/README_EN.md new file mode 100644 index 0000000000000..f6a1cc3965846 --- /dev/null +++ b/solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/README_EN.md @@ -0,0 +1,238 @@ +--- +comments: true +difficulty: Hard +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3231.Minimum%20Number%20of%20Increasing%20Subsequence%20to%20Be%20Removed/README_EN.md +--- + + + +# [3231. Minimum Number of Increasing Subsequence to Be Removed 🔒](https://leetcode.com/problems/minimum-number-of-increasing-subsequence-to-be-removed) + +[中文文档](/solution/3200-3299/3231.Minimum%20Number%20of%20Increasing%20Subsequence%20to%20Be%20Removed/README.md) + +## Description + + + +

Given an array of integers nums, you are allowed to perform the following operation any number of times:

+ +
    +
  • Remove a strictly increasing subsequence from the array.
  • +
+ +

Your task is to find the minimum number of operations required to make the array empty.

+ +

 

+

Example 1:

+ +
+

Input: nums = [5,3,1,4,2]

+ +

Output: 3

+ +

Explanation:

+ +

We remove subsequences [1, 2], [3, 4], [5].

+
+ +

Example 2:

+ +
+

Input: nums = [1,2,3,4,5]

+ +

Output: 1

+
+ +

Example 3:

+ +
+

Input: nums = [5,4,3,2,1]

+ +

Output: 5

+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 105
  • +
+ + + +## Solutions + + + +### Solution 1: Greedy + Binary Search + +We traverse the array $\textit{nums}$ from left to right. For each element $x$, we need to greedily append it after the last element of the preceding sequence that is smaller than $x$. If no such element is found, it means the current element $x$ is smaller than all elements in the preceding sequences, and we need to start a new sequence with $x$. + +From this analysis, we can observe that the last elements of the preceding sequences are in a monotonically decreasing order. Therefore, we can use binary search to find the position of the first element in the preceding sequences that is smaller than $x$, and then place $x$ in that position. + +Finally, we return the number of sequences. + +The time complexity is $O(n \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$. + + + +#### Python3 + +```python +class Solution: + def minOperations(self, nums: List[int]) -> int: + g = [] + for x in nums: + l, r = 0, len(g) + while l < r: + mid = (l + r) >> 1 + if g[mid] < x: + r = mid + else: + l = mid + 1 + if l == len(g): + g.append(x) + else: + g[l] = x + return len(g) +``` + +#### Java + +```java +class Solution { + public int minOperations(int[] nums) { + List g = new ArrayList<>(); + for (int x : nums) { + int l = 0, r = g.size(); + while (l < r) { + int mid = (l + r) >> 1; + if (g.get(mid) < x) { + r = mid; + } else { + l = mid + 1; + } + } + if (l == g.size()) { + g.add(x); + } else { + g.set(l, x); + } + } + return g.size(); + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int minOperations(vector& nums) { + vector g; + for (int x : nums) { + int l = 0, r = g.size(); + while (l < r) { + int mid = (l + r) >> 1; + if (g[mid] < x) { + r = mid; + } else { + l = mid + 1; + } + } + if (l == g.size()) { + g.push_back(x); + } else { + g[l] = x; + } + } + return g.size(); + } +}; +``` + +#### Go + +```go +func minOperations(nums []int) int { + g := []int{} + for _, x := range nums { + l, r := 0, len(g) + for l < r { + mid := (l + r) >> 1 + if g[mid] < x { + r = mid + } else { + l = mid + 1 + } + } + if l == len(g) { + g = append(g, x) + } else { + g[l] = x + } + } + return len(g) +} +``` + +#### TypeScript + +```ts +function minOperations(nums: number[]): number { + const g: number[] = []; + for (const x of nums) { + let [l, r] = [0, g.length]; + while (l < r) { + const mid = (l + r) >> 1; + if (g[mid] < x) { + r = mid; + } else { + l = mid + 1; + } + } + if (l === g.length) { + g.push(x); + } else { + g[l] = x; + } + } + return g.length; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn min_operations(nums: Vec) -> i32 { + let mut g = Vec::new(); + for &x in nums.iter() { + let mut l = 0; + let mut r = g.len(); + while l < r { + let mid = (l + r) / 2; + if g[mid] < x { + r = mid; + } else { + l = mid + 1; + } + } + if l == g.len() { + g.push(x); + } else { + g[l] = x; + } + } + g.len() as i32 + } +} +``` + + + + + + diff --git a/solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/Solution.cpp b/solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/Solution.cpp new file mode 100644 index 0000000000000..75c5d4a2c4ddd --- /dev/null +++ b/solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/Solution.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int minOperations(vector& nums) { + vector g; + for (int x : nums) { + int l = 0, r = g.size(); + while (l < r) { + int mid = (l + r) >> 1; + if (g[mid] < x) { + r = mid; + } else { + l = mid + 1; + } + } + if (l == g.size()) { + g.push_back(x); + } else { + g[l] = x; + } + } + return g.size(); + } +}; \ No newline at end of file diff --git a/solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/Solution.go b/solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/Solution.go new file mode 100644 index 0000000000000..26c6c62dbf432 --- /dev/null +++ b/solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/Solution.go @@ -0,0 +1,20 @@ +func minOperations(nums []int) int { + g := []int{} + for _, x := range nums { + l, r := 0, len(g) + for l < r { + mid := (l + r) >> 1 + if g[mid] < x { + r = mid + } else { + l = mid + 1 + } + } + if l == len(g) { + g = append(g, x) + } else { + g[l] = x + } + } + return len(g) +} \ No newline at end of file diff --git a/solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/Solution.java b/solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/Solution.java new file mode 100644 index 0000000000000..61d01228fba51 --- /dev/null +++ b/solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/Solution.java @@ -0,0 +1,22 @@ +class Solution { + public int minOperations(int[] nums) { + List g = new ArrayList<>(); + for (int x : nums) { + int l = 0, r = g.size(); + while (l < r) { + int mid = (l + r) >> 1; + if (g.get(mid) < x) { + r = mid; + } else { + l = mid + 1; + } + } + if (l == g.size()) { + g.add(x); + } else { + g.set(l, x); + } + } + return g.size(); + } +} \ No newline at end of file diff --git a/solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/Solution.py b/solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/Solution.py new file mode 100644 index 0000000000000..a936fc96e681a --- /dev/null +++ b/solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/Solution.py @@ -0,0 +1,16 @@ +class Solution: + def minOperations(self, nums: List[int]) -> int: + g = [] + for x in nums: + l, r = 0, len(g) + while l < r: + mid = (l + r) >> 1 + if g[mid] < x: + r = mid + else: + l = mid + 1 + if l == len(g): + g.append(x) + else: + g[l] = x + return len(g) diff --git a/solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/Solution.rs b/solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/Solution.rs new file mode 100644 index 0000000000000..43e93ae2b2b62 --- /dev/null +++ b/solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/Solution.rs @@ -0,0 +1,23 @@ +impl Solution { + pub fn min_operations(nums: Vec) -> i32 { + let mut g = Vec::new(); + for &x in nums.iter() { + let mut l = 0; + let mut r = g.len(); + while l < r { + let mid = (l + r) / 2; + if g[mid] < x { + r = mid; + } else { + l = mid + 1; + } + } + if l == g.len() { + g.push(x); + } else { + g[l] = x; + } + } + g.len() as i32 + } +} diff --git a/solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/Solution.ts b/solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/Solution.ts new file mode 100644 index 0000000000000..56f6151197aa9 --- /dev/null +++ b/solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/Solution.ts @@ -0,0 +1,20 @@ +function minOperations(nums: number[]): number { + const g: number[] = []; + for (const x of nums) { + let [l, r] = [0, g.length]; + while (l < r) { + const mid = (l + r) >> 1; + if (g[mid] < x) { + r = mid; + } else { + l = mid + 1; + } + } + if (l === g.length) { + g.push(x); + } else { + g[l] = x; + } + } + return g.length; +} diff --git a/solution/README.md b/solution/README.md index 7b1131802934d..ea7f45e6ccf4b 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3241,6 +3241,7 @@ | 3228 | [将 1 移动到末尾的最大操作次数](/solution/3200-3299/3228.Maximum%20Number%20of%20Operations%20to%20Move%20Ones%20to%20the%20End/README.md) | `贪心`,`字符串`,`计数` | 中等 | 第 407 场周赛 | | 3229 | [使数组等于目标数组所需的最少操作次数](/solution/3200-3299/3229.Minimum%20Operations%20to%20Make%20Array%20Equal%20to%20Target/README.md) | `栈`,`贪心`,`数组`,`动态规划`,`单调栈` | 困难 | 第 407 场周赛 | | 3230 | [客户购买行为分析](/solution/3200-3299/3230.Customer%20Purchasing%20Behavior%20Analysis/README.md) | `数据库` | 中等 | 🔒 | +| 3231 | [Minimum Number of Increasing Subsequence to Be Removed](/solution/3200-3299/3231.Minimum%20Number%20of%20Increasing%20Subsequence%20to%20Be%20Removed/README.md) | | 困难 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index 30e453506478f..0884b9de66021 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3239,6 +3239,7 @@ Press Control + F(or Command + F on | 3228 | [Maximum Number of Operations to Move Ones to the End](/solution/3200-3299/3228.Maximum%20Number%20of%20Operations%20to%20Move%20Ones%20to%20the%20End/README_EN.md) | `Greedy`,`String`,`Counting` | Medium | Weekly Contest 407 | | 3229 | [Minimum Operations to Make Array Equal to Target](/solution/3200-3299/3229.Minimum%20Operations%20to%20Make%20Array%20Equal%20to%20Target/README_EN.md) | `Stack`,`Greedy`,`Array`,`Dynamic Programming`,`Monotonic Stack` | Hard | Weekly Contest 407 | | 3230 | [Customer Purchasing Behavior Analysis](/solution/3200-3299/3230.Customer%20Purchasing%20Behavior%20Analysis/README_EN.md) | `Database` | Medium | 🔒 | +| 3231 | [Minimum Number of Increasing Subsequence to Be Removed](/solution/3200-3299/3231.Minimum%20Number%20of%20Increasing%20Subsequence%20to%20Be%20Removed/README_EN.md) | | Hard | 🔒 | ## Copyright