Skip to content

feat: add solutions to lc problem: No.1671 #2118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

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

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

<!-- tabs:start -->

Expand Down Expand Up @@ -193,8 +193,8 @@ func minimumMountainRemovals(nums []int) int {
```ts
function minimumMountainRemovals(nums: number[]): number {
const n = nums.length;
const left = new Array(n).fill(1);
const right = new Array(n).fill(1);
const left = Array(n).fill(1);
const right = Array(n).fill(1);
for (let i = 1; i < n; ++i) {
for (let j = 0; j < i; ++j) {
if (nums[i] > nums[j]) {
Expand All @@ -219,6 +219,41 @@ function minimumMountainRemovals(nums: number[]): number {
}
```

### **TypeScript**

```ts
impl Solution {
pub fn minimum_mountain_removals(nums: Vec<i32>) -> i32 {
let n = nums.len();
let mut left = vec![1; n];
let mut right = vec![1; n];
for i in 1..n {
for j in 0..i {
if nums[i] > nums[j] {
left[i] = left[i].max(left[j] + 1);
}
}
}
for i in (0..n - 1).rev() {
for j in i + 1..n {
if nums[i] > nums[j] {
right[i] = right[i].max(right[j] + 1);
}
}
}

let mut ans = 0;
for i in 0..n {
if left[i] > 1 && right[i] > 1 {
ans = ans.max(left[i] + right[i] - 1);
}
}

(n as i32) - ans
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@

## Solutions

**Solution 1: Dynamic Programming**

This problem can be transformed into finding the longest increasing subsequence and the longest decreasing subsequence.

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]$.

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$.

The time complexity is $O(n^2)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $nums$.

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -173,8 +183,8 @@ func minimumMountainRemovals(nums []int) int {
```ts
function minimumMountainRemovals(nums: number[]): number {
const n = nums.length;
const left = new Array(n).fill(1);
const right = new Array(n).fill(1);
const left = Array(n).fill(1);
const right = Array(n).fill(1);
for (let i = 1; i < n; ++i) {
for (let j = 0; j < i; ++j) {
if (nums[i] > nums[j]) {
Expand All @@ -199,6 +209,41 @@ function minimumMountainRemovals(nums: number[]): number {
}
```

### **TypeScript**

```ts
impl Solution {
pub fn minimum_mountain_removals(nums: Vec<i32>) -> i32 {
let n = nums.len();
let mut left = vec![1; n];
let mut right = vec![1; n];
for i in 1..n {
for j in 0..i {
if nums[i] > nums[j] {
left[i] = left[i].max(left[j] + 1);
}
}
}
for i in (0..n - 1).rev() {
for j in i + 1..n {
if nums[i] > nums[j] {
right[i] = right[i].max(right[j] + 1);
}
}
}

let mut ans = 0;
for i in 0..n {
if left[i] > 1 && right[i] > 1 {
ans = ans.max(left[i] + right[i] - 1);
}
}

(n as i32) - ans
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
impl Solution {
pub fn minimum_mountain_removals(nums: Vec<i32>) -> i32 {
let n = nums.len();
let mut left = vec![1; n];
let mut right = vec![1; n];
for i in 1..n {
for j in 0..i {
if nums[i] > nums[j] {
left[i] = left[i].max(left[j] + 1);
}
}
}
for i in (0..n - 1).rev() {
for j in i + 1..n {
if nums[i] > nums[j] {
right[i] = right[i].max(right[j] + 1);
}
}
}

let mut ans = 0;
for i in 0..n {
if left[i] > 1 && right[i] > 1 {
ans = ans.max(left[i] + right[i] - 1);
}
}

(n as i32) - ans
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function minimumMountainRemovals(nums: number[]): number {
const n = nums.length;
const left = new Array(n).fill(1);
const right = new Array(n).fill(1);
const left = Array(n).fill(1);
const right = Array(n).fill(1);
for (let i = 1; i < n; ++i) {
for (let j = 0; j < i; ++j) {
if (nums[i] > nums[j]) {
Expand Down