diff --git a/solution/1600-1699/1671.Minimum Number of Removals to Make Mountain Array/README.md b/solution/1600-1699/1671.Minimum Number of Removals to Make Mountain Array/README.md index 92208ac79f890..2591c48e172a2 100644 --- a/solution/1600-1699/1671.Minimum Number of Removals to Make Mountain Array/README.md +++ b/solution/1600-1699/1671.Minimum Number of Removals to Make Mountain Array/README.md @@ -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$ 的长度。 @@ -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]) { @@ -219,6 +219,41 @@ function minimumMountainRemovals(nums: number[]): number { } ``` +### **TypeScript** + +```ts +impl Solution { + pub fn minimum_mountain_removals(nums: Vec) -> 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 + } +} +``` + ### **...** ``` diff --git a/solution/1600-1699/1671.Minimum Number of Removals to Make Mountain Array/README_EN.md b/solution/1600-1699/1671.Minimum Number of Removals to Make Mountain Array/README_EN.md index 46acbc9147780..0c483fbc51c62 100644 --- a/solution/1600-1699/1671.Minimum Number of Removals to Make Mountain Array/README_EN.md +++ b/solution/1600-1699/1671.Minimum Number of Removals to Make Mountain Array/README_EN.md @@ -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$. + ### **Python3** @@ -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]) { @@ -199,6 +209,41 @@ function minimumMountainRemovals(nums: number[]): number { } ``` +### **TypeScript** + +```ts +impl Solution { + pub fn minimum_mountain_removals(nums: Vec) -> 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 + } +} +``` + ### **...** ``` diff --git a/solution/1600-1699/1671.Minimum Number of Removals to Make Mountain Array/Solution.rs b/solution/1600-1699/1671.Minimum Number of Removals to Make Mountain Array/Solution.rs new file mode 100644 index 0000000000000..96e9c643137f4 --- /dev/null +++ b/solution/1600-1699/1671.Minimum Number of Removals to Make Mountain Array/Solution.rs @@ -0,0 +1,30 @@ +impl Solution { + pub fn minimum_mountain_removals(nums: Vec) -> 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 + } +} diff --git a/solution/1600-1699/1671.Minimum Number of Removals to Make Mountain Array/Solution.ts b/solution/1600-1699/1671.Minimum Number of Removals to Make Mountain Array/Solution.ts index cc447ceb23d7f..235ea8f163053 100644 --- a/solution/1600-1699/1671.Minimum Number of Removals to Make Mountain Array/Solution.ts +++ b/solution/1600-1699/1671.Minimum Number of Removals to Make Mountain Array/Solution.ts @@ -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]) {