Skip to content

Add solution and test-cases for problem 2016 #1235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2025
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
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
# [2016.Maximum Difference Between Increasing Elements][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

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]`.

Return the **maximum difference**. If no such `i` and `j` exists, return `-1`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: nums = [7,1,5,4]
Output: 4
Explanation:
The maximum difference occurs with i = 1 and j = 2, nums[j] - nums[i] = 5 - 1 = 4.
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.
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Maximum Difference Between Increasing Elements
```go
```
Input: nums = [9,4,3,2]
Output: -1
Explanation:
There is no i and j such that i < j and nums[i] < nums[j].
```

**Example 3:**

```
Input: nums = [1,5,2,10]
Output: 9
Explanation:
The maximum difference occurs with i = 0 and j = 3, nums[j] - nums[i] = 10 - 1 = 9.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(nums []int) int {
minNumbers := make([]int, len(nums))
minNumbers[0] = nums[0]
for i := 1; i < len(nums); i++ {
minNumbers[i] = min(minNumbers[i-1], nums[i])
}
ans := -1
var diff, a int
for i := len(nums) - 1; i > 0; i-- {
a = minNumbers[i-1]
if a >= nums[i] {
continue
}
diff = nums[i] - a
if ans == -1 || ans < diff {
ans = diff
}
}
return ans
}
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{7, 1, 5, 4}, 4},
{"TestCase2", []int{9, 4, 3, 2}, -1},
{"TestCase3", []int{1, 5, 2, 10}, 9},
}

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

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

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