Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 702d0b1

Browse files
committed
Merge pull request #399 from 0xff-dev/2104
Add solution and test-cases for problem 2104
2 parents c45c772 + d1a6003 commit 702d0b1

File tree

3 files changed

+74
-22
lines changed

3 files changed

+74
-22
lines changed

‎leetcode/2101-2200/2104.Sum-of-Subarray-Ranges/README.md

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,49 @@
11
# [2104.Sum of Subarray Ranges][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 an integer array `nums`. The **range** of a subarray of `nums` is the difference between the largest and smallest element in the subarray.
5+
6+
Return the **sum of all** subarray ranges of `nums`.
7+
8+
A subarray is a contiguous **non-empty** sequence of elements within an array.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: nums = [1,2,3]
14+
Output: 4
15+
Explanation: The 6 subarrays of nums are the following:
16+
[1], range = largest - smallest = 1 - 1 = 0
17+
[2], range = 2 - 2 = 0
18+
[3], range = 3 - 3 = 0
19+
[1,2], range = 2 - 1 = 1
20+
[2,3], range = 3 - 2 = 1
21+
[1,2,3], range = 3 - 1 = 2
22+
So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.
1323
```
1424

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

20-
### 思路1
21-
> ...
22-
Sum of Subarray Ranges
23-
```go
2427
```
28+
Input: nums = [1,3,3]
29+
Output: 4
30+
Explanation: The 6 subarrays of nums are the following:
31+
[1], range = largest - smallest = 1 - 1 = 0
32+
[3], range = 3 - 3 = 0
33+
[3], range = 3 - 3 = 0
34+
[1,3], range = 3 - 1 = 2
35+
[3,3], range = 3 - 3 = 0
36+
[1,3,3], range = 3 - 1 = 2
37+
So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4.
38+
```
39+
40+
**Example 3:**
2541

42+
```
43+
Input: nums = [4,-2,-3,4,1]
44+
Output: 59
45+
Explanation: The sum of all subarray ranges of nums is 59.
46+
```
2647

2748
## 结语
2849

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

3-
func Solution(x bool) bool {
4-
return x
3+
// 看解析可以用单调栈完成
4+
func Solution(nums []int) int64 {
5+
length := len(nums)
6+
max, min := make([]int, length), make([]int, length)
7+
max[0], min[0] = nums[0], nums[0]
8+
for i := 1; i < length; i++ {
9+
max[i] = nums[i-1]
10+
min[i] = nums[i-1]
11+
if nums[i] > max[i] {
12+
max[i] = nums[i]
13+
}
14+
if nums[i] < min[i] {
15+
min[i] = nums[i]
16+
}
17+
}
18+
19+
ans := int64(0)
20+
for l := 2; l <= length; l++ {
21+
m, n := max[l-1], min[l-1]
22+
ans += int64(m - n)
23+
// start
24+
for i := 1; i <= length-l; i++ {
25+
newItem := nums[i+l-1]
26+
if newItem > m {
27+
m = newItem
28+
}
29+
if newItem < n {
30+
n = newItem
31+
}
32+
ans += int64(m - n)
33+
}
34+
}
35+
return ans
536
}

‎leetcode/2101-2200/2104.Sum-of-Subarray-Ranges/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 int64
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{1, 2, 3}, 4},
17+
{"TestCase2", []int{1, 3, 3}, 4},
18+
{"TestCase3", []int{4, -2, -3, 4, 1}, 59},
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)

AltStyle によって変換されたページ (->オリジナル) /