Skip to content

Commit b888a4f

Browse files
authored
Merge pull request #1321 from 0xff-dev/2012
Add solution and test-cases for problem 2012
2 parents a5137e4 + 8922ba1 commit b888a4f

File tree

3 files changed

+59
-22
lines changed

3 files changed

+59
-22
lines changed

leetcode/2001-2100/2012.Sum-of-Beauty-in-the-Array/README.md

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
# [2012.Sum of Beauty in the 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 are given a **0-indexed** integer array nums. For each index `i` (`1 <= i <= nums.length - 2`) the **beauty** of `nums[i]` equals:
5+
6+
- `2`, if `nums[j] < nums[i] < nums[k]`, for **all** `0 <= j < i` and for **all** `i < k <= nums.length - 1`.
7+
- `1`, if `nums[i - 1] < nums[i] < nums[i + 1]`, and the previous condition is not satisfied.
8+
- `0`, if none of the previous conditions holds.
9+
10+
Return the **sum of beauty** of all `nums[i]` where `1 <= i <= nums.length - 2`.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: nums = [1,2,3]
16+
Output: 2
17+
Explanation: For each index i in the range 1 <= i <= 1:
18+
- The beauty of nums[1] equals 2.
1319
```
1420

15-
## 题意
16-
> ...
17-
18-
## 题解
21+
**Example 2:**
1922

20-
### 思路1
21-
> ...
22-
Sum of Beauty in the Array
23-
```go
2423
```
24+
Input: nums = [2,4,6,4]
25+
Output: 1
26+
Explanation: For each index i in the range 1 <= i <= 2:
27+
- The beauty of nums[1] equals 1.
28+
- The beauty of nums[2] equals 0.
29+
```
30+
31+
**Example 3:**
2532

33+
```
34+
Input: nums = [3,2,1]
35+
Output: 0
36+
Explanation: For each index i in the range 1 <= i <= 1:
37+
- The beauty of nums[1] equals 0.
38+
```
2639

2740
## 结语
2841

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) int {
4+
l := len(nums)
5+
6+
leftMax := make([]int, l)
7+
rightMin := make([]int, l)
8+
leftMax[0] = nums[0]
9+
for i := 1; i < l; i++ {
10+
leftMax[i] = max(nums[i-1], leftMax[i-1])
11+
}
12+
rightMin[l-1] = nums[l-1]
13+
for i := l - 2; i >= 0; i-- {
14+
rightMin[i] = min(rightMin[i+1], nums[i+1])
15+
}
16+
ret := 0
17+
for i := 1; i < l-1; i++ {
18+
lm := leftMax[i]
19+
rm := rightMin[i]
20+
if nums[i] > lm && nums[i] < rm {
21+
ret += 2
22+
continue
23+
}
24+
if nums[i] > nums[i-1] && nums[i] < nums[i+1] {
25+
ret++
26+
}
27+
}
28+
return ret
529
}

leetcode/2001-2100/2012.Sum-of-Beauty-in-the-Array/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ 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{1, 2, 3}, 2},
17+
{"TestCase2", []int{2, 4, 6, 4}, 1},
18+
{"TestCase3", []int{3, 2, 1}, 0},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)