Skip to content

Commit bc0da71

Browse files
committed
+ problem 1574
1 parent 126ad6d commit bc0da71

File tree

5 files changed

+181
-0
lines changed

5 files changed

+181
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# 1574. Shortest Subarray to be Removed to Make Array Sorted
2+
Given an integer array `arr`, remove a subarray (can be empty) from `arr` such that the remaining elements in `arr` are **non-decreasing**.
3+
4+
Return *the length of the shortest subarray to remove*.
5+
6+
A **subarray** is a contiguous subsequence of the array.
7+
8+
#### Example 1:
9+
<pre>
10+
<strong>Input:</strong> arr = [1,2,3,10,4,2,3,5]
11+
<strong>Output:</strong> 3
12+
<strong>Explanation:</strong> The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted.
13+
Another correct solution is to remove the subarray [3,10,4].
14+
</pre>
15+
16+
#### Example 2:
17+
<pre>
18+
<strong>Input:</strong> arr = [5,4,3,2,1]
19+
<strong>Output:</strong> 4
20+
<strong>Explanation:</strong> Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1].
21+
</pre>
22+
23+
#### Example 3:
24+
<pre>
25+
<strong>Input:</strong> arr = [1,2,3]
26+
<strong>Output:</strong> 0
27+
<strong>Explanation:</strong> The array is already non-decreasing. We do not need to remove any elements.
28+
</pre>
29+
30+
#### Constraints:
31+
* <code>1 <= arr.length <= 10<sup>5</sup></code>
32+
* <code>0 <= arr[i] <= 10<sup>9</sup></code>
33+
34+
## Solutions (Rust)
35+
36+
### 1. Solution
37+
```Rust
38+
impl Solution {
39+
pub fn find_length_of_shortest_subarray(arr: Vec<i32>) -> i32 {
40+
let mut arr = arr;
41+
let mut i = arr.len();
42+
let mut ret = arr.len() - 1;
43+
44+
arr.insert(0, 0);
45+
46+
while i > 0 && arr[i - 1] <= arr[i] {
47+
i -= 1;
48+
}
49+
50+
if i == 0 {
51+
return 0;
52+
}
53+
54+
for j in 0..arr.len() {
55+
if j > 0 && arr[j - 1] > arr[j] {
56+
break;
57+
}
58+
59+
while i < arr.len() && arr[i] < arr[j] {
60+
i += 1;
61+
}
62+
63+
ret = ret.min(i - j - 1);
64+
}
65+
66+
ret as i32
67+
}
68+
}
69+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# 1574. 删除最短的子数组使剩余数组有序
2+
给你一个整数数组 `arr` ,请你删除一个子数组(可以为空),使得 `arr` 中剩下的元素是 **非递减** 的。
3+
4+
一个子数组指的是原数组中连续的一个子序列。
5+
6+
请你返回满足题目要求的最短子数组的长度。
7+
8+
#### 示例 1:
9+
<pre>
10+
<strong>输入:</strong> arr = [1,2,3,10,4,2,3,5]
11+
<strong>输出:</strong> 3
12+
<strong>解释:</strong> 我们需要删除的最短子数组是 [10,4,2] ,长度为 3 。剩余元素形成非递减数组 [1,2,3,3,5] 。
13+
另一个正确的解为删除子数组 [3,10,4] 。
14+
</pre>
15+
16+
#### 示例 2:
17+
<pre>
18+
<strong>输入:</strong> arr = [5,4,3,2,1]
19+
<strong>输出:</strong> 4
20+
<strong>解释:</strong> 由于数组是严格递减的,我们只能保留一个元素。所以我们需要删除长度为 4 的子数组,要么删除 [5,4,3,2],要么删除 [4,3,2,1]。
21+
</pre>
22+
23+
#### 示例 3:
24+
<pre>
25+
<strong>输入:</strong> arr = [1,2,3]
26+
<strong>输出:</strong> 0
27+
<strong>解释:</strong> 数组已经是非递减的了,我们不需要删除任何元素。
28+
</pre>
29+
30+
#### 示例 4:
31+
<pre>
32+
<strong>输入:</strong> arr = [1]
33+
<strong>输出:</strong> 0
34+
</pre>
35+
36+
#### 提示:
37+
* <code>1 <= arr.length <= 10<sup>5</sup></code>
38+
* <code>0 <= arr[i] <= 10<sup>9</sup></code>
39+
40+
## 题解 (Rust)
41+
42+
### 1. 题解
43+
```Rust
44+
impl Solution {
45+
pub fn find_length_of_shortest_subarray(arr: Vec<i32>) -> i32 {
46+
let mut arr = arr;
47+
let mut i = arr.len();
48+
let mut ret = arr.len() - 1;
49+
50+
arr.insert(0, 0);
51+
52+
while i > 0 && arr[i - 1] <= arr[i] {
53+
i -= 1;
54+
}
55+
56+
if i == 0 {
57+
return 0;
58+
}
59+
60+
for j in 0..arr.len() {
61+
if j > 0 && arr[j - 1] > arr[j] {
62+
break;
63+
}
64+
65+
while i < arr.len() && arr[i] < arr[j] {
66+
i += 1;
67+
}
68+
69+
ret = ret.min(i - j - 1);
70+
}
71+
72+
ret as i32
73+
}
74+
}
75+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
impl Solution {
2+
pub fn find_length_of_shortest_subarray(arr: Vec<i32>) -> i32 {
3+
let mut arr = arr;
4+
let mut i = arr.len();
5+
let mut ret = arr.len() - 1;
6+
7+
arr.insert(0, 0);
8+
9+
while i > 0 && arr[i - 1] <= arr[i] {
10+
i -= 1;
11+
}
12+
13+
if i == 0 {
14+
return 0;
15+
}
16+
17+
for j in 0..arr.len() {
18+
if j > 0 && arr[j - 1] > arr[j] {
19+
break;
20+
}
21+
22+
while i < arr.len() && arr[i] < arr[j] {
23+
i += 1;
24+
}
25+
26+
ret = ret.min(i - j - 1);
27+
}
28+
29+
ret as i32
30+
}
31+
}

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,7 @@
838838
[1567][1567l]|[Maximum Length of Subarray With Positive Product][1567] |![rs]
839839
[1572][1572l]|[Matrix Diagonal Sum][1572] |![rs]
840840
[1573][1573l]|[Number of Ways to Split a String][1573] |![rs]
841+
[1574][1574l]|[Shortest Subarray to be Removed to Make Array Sorted][1574] |![rs]
841842
[1576][1576l]|[Replace All ?'s to Avoid Consecutive Repeating Characters][1576] |![rs]
842843
[1577][1577l]|[Number of Ways Where Square of Number Is Equal to Product of Two Numbers][1577] |![py]
843844
[1578][1578l]|[Minimum Time to Make Rope Colorful][1578] |![rs]
@@ -2127,6 +2128,7 @@
21272128
[1567]:Problemset/1567-Maximum%20Length%20of%20Subarray%20With%20Positive%20Product/README.md#1567-maximum-length-of-subarray-with-positive-product
21282129
[1572]:Problemset/1572-Matrix%20Diagonal%20Sum/README.md#1572-matrix-diagonal-sum
21292130
[1573]:Problemset/1573-Number%20of%20Ways%20to%20Split%20a%20String/README.md#1573-number-of-ways-to-split-a-string
2131+
[1574]:Problemset/1574-Shortest%20Subarray%20to%20be%20Removed%20to%20Make%20Array%20Sorted/README.md#1574-shortest-subarray-to-be-removed-to-make-array-sorted
21302132
[1576]:Problemset/1576-Replace%20All%20%3F's%20to%20Avoid%20Consecutive%20Repeating%20Characters/README.md#1576-replace-all-s-to-avoid-consecutive-repeating-characters
21312133
[1577]:Problemset/1577-Number%20of%20Ways%20Where%20Square%20of%20Number%20Is%20Equal%20to%20Product%20of%20Two%20Numbers/README.md#1577-number-of-ways-where-square-of-number-is-equal-to-product-of-two-numbers
21322134
[1578]:Problemset/1578-Minimum%20Time%20to%20Make%20Rope%20Colorful/README.md#1578-minimum-time-to-make-rope-colorful
@@ -3419,6 +3421,7 @@
34193421
[1567l]:https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/
34203422
[1572l]:https://leetcode.com/problems/matrix-diagonal-sum/
34213423
[1573l]:https://leetcode.com/problems/number-of-ways-to-split-a-string/
3424+
[1574l]:https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/
34223425
[1576l]:https://leetcode.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/
34233426
[1577l]:https://leetcode.com/problems/number-of-ways-where-square-of-number-is-equal-to-product-of-two-numbers/
34243427
[1578l]:https://leetcode.com/problems/minimum-time-to-make-rope-colorful/

README_CN.md

+3
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,7 @@
838838
[1567][1567l]|[乘积为正数的最长子数组长度][1567] |![rs]
839839
[1572][1572l]|[矩阵对角线元素的和][1572] |![rs]
840840
[1573][1573l]|[分割字符串的方案数][1573] |![rs]
841+
[1574][1574l]|[删除最短的子数组使剩余数组有序][1574] |![rs]
841842
[1576][1576l]|[替换所有的问号][1576] |![rs]
842843
[1577][1577l]|[数的平方等于两数乘积的方法数][1577] |![py]
843844
[1578][1578l]|[使绳子变成彩色的最短时间][1578] |![rs]
@@ -2127,6 +2128,7 @@
21272128
[1567]:Problemset/1567-Maximum%20Length%20of%20Subarray%20With%20Positive%20Product/README_CN.md#1567-乘积为正数的最长子数组长度
21282129
[1572]:Problemset/1572-Matrix%20Diagonal%20Sum/README_CN.md#1572-矩阵对角线元素的和
21292130
[1573]:Problemset/1573-Number%20of%20Ways%20to%20Split%20a%20String/README_CN.md#1573-分割字符串的方案数
2131+
[1574]:Problemset/1574-Shortest%20Subarray%20to%20be%20Removed%20to%20Make%20Array%20Sorted/README_CN.md#1574-删除最短的子数组使剩余数组有序
21302132
[1576]:Problemset/1576-Replace%20All%20%3F's%20to%20Avoid%20Consecutive%20Repeating%20Characters/README_CN.md#1576-替换所有的问号
21312133
[1577]:Problemset/1577-Number%20of%20Ways%20Where%20Square%20of%20Number%20Is%20Equal%20to%20Product%20of%20Two%20Numbers/README_CN.md#1577-数的平方等于两数乘积的方法数
21322134
[1578]:Problemset/1578-Minimum%20Time%20to%20Make%20Rope%20Colorful/README_CN.md#1578-使绳子变成彩色的最短时间
@@ -3419,6 +3421,7 @@
34193421
[1567l]:https://leetcode.cn/problems/maximum-length-of-subarray-with-positive-product/
34203422
[1572l]:https://leetcode.cn/problems/matrix-diagonal-sum/
34213423
[1573l]:https://leetcode.cn/problems/number-of-ways-to-split-a-string/
3424+
[1574l]:https://leetcode.cn/problems/shortest-subarray-to-be-removed-to-make-array-sorted/
34223425
[1576l]:https://leetcode.cn/problems/replace-all-s-to-avoid-consecutive-repeating-characters/
34233426
[1577l]:https://leetcode.cn/problems/number-of-ways-where-square-of-number-is-equal-to-product-of-two-numbers/
34243427
[1578l]:https://leetcode.cn/problems/minimum-time-to-make-rope-colorful/

0 commit comments

Comments
 (0)