Skip to content

Commit d47417a

Browse files
authored
Merge pull request #1169 from 0xff-dev/845
Add solution and test-cases for problem 845
2 parents ab2bddf + 2113251 commit d47417a

File tree

3 files changed

+47
-23
lines changed

3 files changed

+47
-23
lines changed

leetcode/801-900/0845.Longest-Mountain-in-Array/README.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
# [845.Longest Mountain in Array][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You may recall that an array `arr` is a **mountain array** if and only if:
5+
6+
- `arr.length >= 3`
7+
- There exists some index `i` **(0-indexed)** with `0 < i < arr.length - 1` such that:
8+
9+
- `arr[0] < arr[1] < ... < arr[i - 1] < arr[i]`
10+
- `arr[i] > arr[i + 1] > ... > arr[arr.length - 1]`
11+
12+
Given an integer array `arr`, return the length of the longest subarray, which is a mountain. Return `0` if there is no mountain subarray.
713

814
**Example 1:**
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: arr = [2,1,4,7,3,2,5]
18+
Output: 5
19+
Explanation: The largest mountain is [1,4,7,3,2] which has length 5.
1320
```
1421

15-
## 题意
16-
> ...
22+
**Example 2:**
1723

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Longest Mountain in Array
23-
```go
2424
```
25-
25+
Input: arr = [2,2,2]
26+
Output: 0
27+
Explanation: There is no mountain.
28+
```
2629

2730
## 结语
2831

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(arr []int) int {
4+
left := make([]int, len(arr))
5+
left[0] = 0
6+
for i := 1; i < len(arr); i++ {
7+
if arr[i] > arr[i-1] {
8+
left[i] = left[i-1] + 1
9+
}
10+
}
11+
ans := 0
12+
cnt := 0
13+
for i := len(arr) - 2; i > 0; i-- {
14+
if arr[i] > arr[i+1] {
15+
cnt++
16+
} else {
17+
cnt = 0
18+
}
19+
if left[i] != 0 && cnt != 0 {
20+
ans = max(ans, left[i]+cnt)
21+
}
22+
}
23+
if ans > 0 {
24+
ans++
25+
}
26+
return ans
527
}

leetcode/801-900/0845.Longest-Mountain-in-Array/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{2, 1, 4, 7, 3, 2, 5}, 5},
17+
{"TestCase2", []int{2, 2, 2}, 0},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)