Skip to content

Commit 468e051

Browse files
authored
feat: add solutions to lc problem: No.1671 (#2118)
No.1671.Minimum Number of Removals to Make Mountain Array
1 parent ba0e3f2 commit 468e051

File tree

4 files changed

+117
-7
lines changed

4 files changed

+117
-7
lines changed

solution/1600-1699/1671.Minimum Number of Removals to Make Mountain Array/README.md

+38-3
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060

6161
那么最终答案就是 $n - \max(left[i] + right[i] - 1)$,其中 $1 \leq i \leq n$,并且 $left[i] \gt 1$ 且 $right[i] \gt 1$。
6262

63-
时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `nums` 的长度。
63+
时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。
6464

6565
<!-- tabs:start -->
6666

@@ -193,8 +193,8 @@ func minimumMountainRemovals(nums []int) int {
193193
```ts
194194
function minimumMountainRemovals(nums: number[]): number {
195195
const n = nums.length;
196-
const left = new Array(n).fill(1);
197-
const right = new Array(n).fill(1);
196+
const left = Array(n).fill(1);
197+
const right = Array(n).fill(1);
198198
for (let i = 1; i < n; ++i) {
199199
for (let j = 0; j < i; ++j) {
200200
if (nums[i] > nums[j]) {
@@ -219,6 +219,41 @@ function minimumMountainRemovals(nums: number[]): number {
219219
}
220220
```
221221

222+
### **TypeScript**
223+
224+
```ts
225+
impl Solution {
226+
pub fn minimum_mountain_removals(nums: Vec<i32>) -> i32 {
227+
let n = nums.len();
228+
let mut left = vec![1; n];
229+
let mut right = vec![1; n];
230+
for i in 1..n {
231+
for j in 0..i {
232+
if nums[i] > nums[j] {
233+
left[i] = left[i].max(left[j] + 1);
234+
}
235+
}
236+
}
237+
for i in (0..n - 1).rev() {
238+
for j in i + 1..n {
239+
if nums[i] > nums[j] {
240+
right[i] = right[i].max(right[j] + 1);
241+
}
242+
}
243+
}
244+
245+
let mut ans = 0;
246+
for i in 0..n {
247+
if left[i] > 1 && right[i] > 1 {
248+
ans = ans.max(left[i] + right[i] - 1);
249+
}
250+
}
251+
252+
(n as i32) - ans
253+
}
254+
}
255+
```
256+
222257
### **...**
223258

224259
```

solution/1600-1699/1671.Minimum Number of Removals to Make Mountain Array/README_EN.md

+47-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@
4646

4747
## Solutions
4848

49+
**Solution 1: Dynamic Programming**
50+
51+
This problem can be transformed into finding the longest increasing subsequence and the longest decreasing subsequence.
52+
53+
We define $left[i]$ as the length of the longest increasing subsequence ending with $nums[i]$, and define $right[i]$ as the length of the longest decreasing subsequence starting with $nums[i]$.
54+
55+
Then the final answer is $n - \max(left[i] + right[i] - 1)$, where $1 \leq i \leq n$, and $left[i] \gt 1$ and $right[i] \gt 1$.
56+
57+
The time complexity is $O(n^2)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $nums$.
58+
4959
<!-- tabs:start -->
5060

5161
### **Python3**
@@ -173,8 +183,8 @@ func minimumMountainRemovals(nums []int) int {
173183
```ts
174184
function minimumMountainRemovals(nums: number[]): number {
175185
const n = nums.length;
176-
const left = new Array(n).fill(1);
177-
const right = new Array(n).fill(1);
186+
const left = Array(n).fill(1);
187+
const right = Array(n).fill(1);
178188
for (let i = 1; i < n; ++i) {
179189
for (let j = 0; j < i; ++j) {
180190
if (nums[i] > nums[j]) {
@@ -199,6 +209,41 @@ function minimumMountainRemovals(nums: number[]): number {
199209
}
200210
```
201211

212+
### **TypeScript**
213+
214+
```ts
215+
impl Solution {
216+
pub fn minimum_mountain_removals(nums: Vec<i32>) -> i32 {
217+
let n = nums.len();
218+
let mut left = vec![1; n];
219+
let mut right = vec![1; n];
220+
for i in 1..n {
221+
for j in 0..i {
222+
if nums[i] > nums[j] {
223+
left[i] = left[i].max(left[j] + 1);
224+
}
225+
}
226+
}
227+
for i in (0..n - 1).rev() {
228+
for j in i + 1..n {
229+
if nums[i] > nums[j] {
230+
right[i] = right[i].max(right[j] + 1);
231+
}
232+
}
233+
}
234+
235+
let mut ans = 0;
236+
for i in 0..n {
237+
if left[i] > 1 && right[i] > 1 {
238+
ans = ans.max(left[i] + right[i] - 1);
239+
}
240+
}
241+
242+
(n as i32) - ans
243+
}
244+
}
245+
```
246+
202247
### **...**
203248

204249
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
impl Solution {
2+
pub fn minimum_mountain_removals(nums: Vec<i32>) -> i32 {
3+
let n = nums.len();
4+
let mut left = vec![1; n];
5+
let mut right = vec![1; n];
6+
for i in 1..n {
7+
for j in 0..i {
8+
if nums[i] > nums[j] {
9+
left[i] = left[i].max(left[j] + 1);
10+
}
11+
}
12+
}
13+
for i in (0..n - 1).rev() {
14+
for j in i + 1..n {
15+
if nums[i] > nums[j] {
16+
right[i] = right[i].max(right[j] + 1);
17+
}
18+
}
19+
}
20+
21+
let mut ans = 0;
22+
for i in 0..n {
23+
if left[i] > 1 && right[i] > 1 {
24+
ans = ans.max(left[i] + right[i] - 1);
25+
}
26+
}
27+
28+
(n as i32) - ans
29+
}
30+
}

solution/1600-1699/1671.Minimum Number of Removals to Make Mountain Array/Solution.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function minimumMountainRemovals(nums: number[]): number {
22
const n = nums.length;
3-
const left = new Array(n).fill(1);
4-
const right = new Array(n).fill(1);
3+
const left = Array(n).fill(1);
4+
const right = Array(n).fill(1);
55
for (let i = 1; i < n; ++i) {
66
for (let j = 0; j < i; ++j) {
77
if (nums[i] > nums[j]) {

0 commit comments

Comments
 (0)