Skip to content
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
39 changes: 26 additions & 13 deletions leetcode/2001-2100/2012.Sum-of-Beauty-in-the-Array/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
# [2012.Sum of Beauty in the 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 are given a **0-indexed** integer array nums. For each index `i` (`1 <= i <= nums.length - 2`) the **beauty** of `nums[i]` equals:

- `2`, if `nums[j] < nums[i] < nums[k]`, for **all** `0 <= j < i` and for **all** `i < k <= nums.length - 1`.
- `1`, if `nums[i - 1] < nums[i] < nums[i + 1]`, and the previous condition is not satisfied.
- `0`, if none of the previous conditions holds.

Return the **sum of beauty** of all `nums[i]` where `1 <= i <= nums.length - 2`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: nums = [1,2,3]
Output: 2
Explanation: For each index i in the range 1 <= i <= 1:
- The beauty of nums[1] equals 2.
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Sum of Beauty in the Array
```go
```
Input: nums = [2,4,6,4]
Output: 1
Explanation: For each index i in the range 1 <= i <= 2:
- The beauty of nums[1] equals 1.
- The beauty of nums[2] equals 0.
```

**Example 3:**

```
Input: nums = [3,2,1]
Output: 0
Explanation: For each index i in the range 1 <= i <= 1:
- The beauty of nums[1] equals 0.
```

## 结语

Expand Down
28 changes: 26 additions & 2 deletions leetcode/2001-2100/2012.Sum-of-Beauty-in-the-Array/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(nums []int) int {
l := len(nums)

leftMax := make([]int, l)
rightMin := make([]int, l)
leftMax[0] = nums[0]
for i := 1; i < l; i++ {
leftMax[i] = max(nums[i-1], leftMax[i-1])
}
rightMin[l-1] = nums[l-1]
for i := l - 2; i >= 0; i-- {
rightMin[i] = min(rightMin[i+1], nums[i+1])
}
ret := 0
for i := 1; i < l-1; i++ {
lm := leftMax[i]
rm := rightMin[i]
if nums[i] > lm && nums[i] < rm {
ret += 2
continue
}
if nums[i] > nums[i-1] && nums[i] < nums[i+1] {
ret++
}
}
return ret
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ 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, 2, 3}, 2},
{"TestCase2", []int{2, 4, 6, 4}, 1},
{"TestCase3", []int{3, 2, 1}, 0},
}

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

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

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