Skip to content

Commit dea1851

Browse files
authored
Merge pull request #1235 from 0xff-dev/2016
Add solution and test-cases for problem 2016
2 parents c2f9d01 + 0a2a226 commit dea1851

File tree

3 files changed

+49
-22
lines changed

3 files changed

+49
-22
lines changed

leetcode/2001-2100/2016.Maximum-Difference-Between-Increasing-Elements/README.md

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
# [2016.Maximum Difference Between Increasing Elements][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
74

5+
Given a **0-indexed** integer array `nums` of size `n`, find the **maximum difference** between `nums[i]` and `nums[j]` (i.e., `nums[j] - nums[i]`), such that `0 <= i < j < n` and `nums[i] < nums[j]`.
6+
7+
Return the **maximum difference**. If no such `i` and `j` exists, return `-1`.
8+
89
**Example 1:**
910

1011
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
12+
Input: nums = [7,1,5,4]
13+
Output: 4
14+
Explanation:
15+
The maximum difference occurs with i = 1 and j = 2, nums[j] - nums[i] = 5 - 1 = 4.
16+
Note that with i = 1 and j = 0, the difference nums[j] - nums[i] = 7 - 1 = 6, but i > j, so it is not valid.
1317
```
1418

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

20-
### 思路1
21-
> ...
22-
Maximum Difference Between Increasing Elements
23-
```go
2421
```
22+
Input: nums = [9,4,3,2]
23+
Output: -1
24+
Explanation:
25+
There is no i and j such that i < j and nums[i] < nums[j].
26+
```
27+
28+
**Example 3:**
2529

30+
```
31+
Input: nums = [1,5,2,10]
32+
Output: 9
33+
Explanation:
34+
The maximum difference occurs with i = 0 and j = 3, nums[j] - nums[i] = 10 - 1 = 9.
35+
```
2636

2737
## 结语
2838

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) int {
4+
minNumbers := make([]int, len(nums))
5+
minNumbers[0] = nums[0]
6+
for i := 1; i < len(nums); i++ {
7+
minNumbers[i] = min(minNumbers[i-1], nums[i])
8+
}
9+
ans := -1
10+
var diff, a int
11+
for i := len(nums) - 1; i > 0; i-- {
12+
a = minNumbers[i-1]
13+
if a >= nums[i] {
14+
continue
15+
}
16+
diff = nums[i] - a
17+
if ans == -1 || ans < diff {
18+
ans = diff
19+
}
20+
}
21+
return ans
522
}

leetcode/2001-2100/2016.Maximum-Difference-Between-Increasing-Elements/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{7, 1, 5, 4}, 4},
17+
{"TestCase2", []int{9, 4, 3, 2}, -1},
18+
{"TestCase3", []int{1, 5, 2, 10}, 9},
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)