Skip to content
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

Add solution and test-cases for problem 1671 #1020

Merged
merged 1 commit into from
Oct 30, 2024
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
@@ -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].
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading