diff --git a/leetcode/1601-1700/1671.Minimum-Number-of-Removals-to-Make-Mountain-Array/README.md b/leetcode/1601-1700/1671.Minimum-Number-of-Removals-to-Make-Mountain-Array/README.md index 24e6f92bb..c805eb70a 100755 --- a/leetcode/1601-1700/1671.Minimum-Number-of-Removals-to-Make-Mountain-Array/README.md +++ b/leetcode/1601-1700/1671.Minimum-Number-of-Removals-to-Make-Mountain-Array/README.md @@ -1,28 +1,31 @@ # [1671.Minimum Number of Removals to Make Mountain Array][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +You may recall that an array `arr` is a **mountain array** if and only if: + +- `arr.length >= 3` +- There exists some index `i` (**0-indexed**) with `0 < i < arr.length - 1` such that: + + - `arr[0] < arr[1] < ... < arr[i - 1] < arr[i]` + - `arr[i] > arr[i + 1] > ... > arr[arr.length - 1]` + +Given an integer array `nums`, return the **minimum** number of elements to remove to make `nums` a **mountain array**. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [1,3,1] +Output: 0 +Explanation: The array itself is a mountain array so we do not need to remove any elements. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Minimum Number of Removals to Make Mountain Array -```go ``` - +Input: nums = [2,1,1,5,6,2,3,1] +Output: 3 +Explanation: One solution is to remove the elements at indices 0, 1, and 5, making the array nums = [1,5,6,3,1]. +``` ## 结语 diff --git a/leetcode/1601-1700/1671.Minimum-Number-of-Removals-to-Make-Mountain-Array/Solution.go b/leetcode/1601-1700/1671.Minimum-Number-of-Removals-to-Make-Mountain-Array/Solution.go index d115ccf5e..1bdebad00 100644 --- a/leetcode/1601-1700/1671.Minimum-Number-of-Removals-to-Make-Mountain-Array/Solution.go +++ b/leetcode/1601-1700/1671.Minimum-Number-of-Removals-to-Make-Mountain-Array/Solution.go @@ -1,5 +1,36 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int) int { + l := len(nums) + if l < 3 { + return 0 + } + left, right := make([]int, l), make([]int, l) + left[0] = 1 + for i := 1; i < l; i++ { + left[i] = 1 + for pre := i - 1; pre >= 0; pre-- { + if nums[pre] < nums[i] { + left[i] = max(left[i], left[pre]+1) + } + } + } + right[l-1] = 1 + for i := l - 2; i >= 0; i-- { + right[i] = 1 + for next := i + 1; next < l; next++ { + if nums[i] > nums[next] { + right[i] = max(right[i], right[next]+1) + } + } + } + ans := 0 + for i := 1; i < l-1; i++ { + if left[i] == 1 || right[i] == 1 { + continue + } + now := left[i] + right[i] - 1 + ans = max(ans, now) + } + return l - ans } diff --git a/leetcode/1601-1700/1671.Minimum-Number-of-Removals-to-Make-Mountain-Array/Solution_test.go b/leetcode/1601-1700/1671.Minimum-Number-of-Removals-to-Make-Mountain-Array/Solution_test.go index 14ff50eb4..0ac25d4f9 100644 --- a/leetcode/1601-1700/1671.Minimum-Number-of-Removals-to-Make-Mountain-Array/Solution_test.go +++ b/leetcode/1601-1700/1671.Minimum-Number-of-Removals-to-Make-Mountain-Array/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, 3, 1}, 0}, + {"TestCase3", []int{2, 1, 1, 5, 6, 2, 3, 1}, 3}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }