Skip to content

Commit 786860c

Browse files
committed
feat: add solutions to lc problem: No.2016
No.2016.Maximum Difference Between Increasing Elements
1 parent 6646f0e commit 786860c

File tree

7 files changed

+189
-123
lines changed

7 files changed

+189
-123
lines changed

solution/2000-2099/2016.Maximum Difference Between Increasing Elements/README.md

+78-53
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,15 @@
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54-
此题并不能单纯遍历数组,寻找其中的最大最小值,然后计算差,因为需要保证最大最小值的前后顺序。
54+
**方法一:维护前缀最小值**
5555

56-
**步骤:**
56+
我们用变量 $mi$ 表示当前遍历到的元素中的最小值,用变量 $ans$ 表示最大差值,初始时 $mi$ 为 $+\infty$,而 $ans$ 为 $-1$。
5757

58-
1. 只维护最小值与最大差值(返回值)。
59-
2. 遍历数组,当遍历元素比最小值大时,与最小值比较,更新最大差值。
60-
3. 否则更新最小值。
58+
遍历数组,对于当前遍历到的元素 $x$,如果 $x \gt mi$,则更新 $ans$ 为 $max(ans, x - mi)$,否则更新 $mi = x$。
59+
60+
遍历结束后,返回 $ans$。
61+
62+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组的长度。
6163

6264
<!-- tabs:start -->
6365

@@ -68,13 +70,13 @@
6870
```python
6971
class Solution:
7072
def maximumDifference(self, nums: List[int]) -> int:
71-
mi = nums[0]
72-
ans, n = -1, len(nums)
73-
for i in range(1, n):
74-
if nums[i] > mi:
75-
ans = max(ans, nums[i] - mi)
73+
mi = inf
74+
ans = -1
75+
for x in nums:
76+
if x > mi:
77+
ans = max(ans, x - mi)
7678
else:
77-
mi = nums[i]
79+
mi = x
7880
return ans
7981
```
8082

@@ -85,20 +87,64 @@ class Solution:
8587
```java
8688
class Solution {
8789
public int maximumDifference(int[] nums) {
88-
int mi = nums[0];
90+
int mi = 1 << 30;
8991
int ans = -1;
90-
for (int i = 1; i < nums.length; ++i) {
91-
if (nums[i] > mi) {
92-
ans = Math.max(ans, nums[i] - mi);
92+
for (int x : nums) {
93+
if (x > mi) {
94+
ans = Math.max(ans, x - mi);
9395
} else {
94-
mi = nums[i];
96+
mi = x;
9597
}
9698
}
9799
return ans;
98100
}
99101
}
100102
```
101103

104+
### **C++**
105+
106+
```cpp
107+
class Solution {
108+
public:
109+
int maximumDifference(vector<int>& nums) {
110+
int mi = 1 << 30;
111+
int ans = -1;
112+
for (int& x : nums) {
113+
if (x > mi) {
114+
ans = max(ans, x - mi);
115+
} else {
116+
mi = x;
117+
}
118+
}
119+
return ans;
120+
}
121+
};
122+
```
123+
124+
### **Go**
125+
126+
```go
127+
func maximumDifference(nums []int) int {
128+
mi := 1 << 30
129+
ans := -1
130+
for _, x := range nums {
131+
if mi < x {
132+
ans = max(ans, x-mi)
133+
} else {
134+
mi = x
135+
}
136+
}
137+
return ans
138+
}
139+
140+
func max(a, b int) int {
141+
if a > b {
142+
return a
143+
}
144+
return b
145+
}
146+
```
147+
102148
### **TypeScript**
103149

104150
```ts
@@ -133,48 +179,27 @@ impl Solution {
133179
}
134180
```
135181

136-
### **C++**
137-
138-
```cpp
139-
class Solution {
140-
public:
141-
int maximumDifference(vector<int>& nums) {
142-
int mi = nums[0];
143-
int ans = -1;
144-
for (int i = 1, n = nums.size(); i < n; ++i) {
145-
if (nums[i] > mi)
146-
ans = max(ans, nums[i] - mi);
147-
else
148-
mi = nums[i];
182+
### **JavaScript**
183+
184+
```js
185+
/**
186+
* @param {number[]} nums
187+
* @return {number}
188+
*/
189+
var maximumDifference = function (nums) {
190+
let mi = 1 << 30;
191+
let ans = -1;
192+
for (const x of nums) {
193+
if (mi < x) {
194+
ans = Math.max(ans, x - mi);
195+
} else {
196+
mi = x;
149197
}
150-
return ans;
151198
}
199+
return ans;
152200
};
153201
```
154202

155-
### **Go**
156-
157-
```go
158-
func maximumDifference(nums []int) int {
159-
mi, ans := nums[0], -1
160-
for i, n := 1, len(nums); i < n; i++ {
161-
if nums[i] > mi {
162-
ans = max(ans, nums[i]-mi)
163-
} else {
164-
mi = nums[i]
165-
}
166-
}
167-
return ans
168-
}
169-
170-
func max(a, b int) int {
171-
if a > b {
172-
return a
173-
}
174-
return b
175-
}
176-
```
177-
178203
### **...**
179204

180205
```

solution/2000-2099/2016.Maximum Difference Between Increasing Elements/README_EN.md

+71-48
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ The maximum difference occurs with i = 0 and j = 3, nums[j] - nums[i] = 10 - 1 =
5555
```python
5656
class Solution:
5757
def maximumDifference(self, nums: List[int]) -> int:
58-
mi = nums[0]
59-
ans, n = -1, len(nums)
60-
for i in range(1, n):
61-
if nums[i] > mi:
62-
ans = max(ans, nums[i] - mi)
58+
mi = inf
59+
ans = -1
60+
for x in nums:
61+
if x > mi:
62+
ans = max(ans, x - mi)
6363
else:
64-
mi = nums[i]
64+
mi = x
6565
return ans
6666
```
6767

@@ -70,20 +70,64 @@ class Solution:
7070
```java
7171
class Solution {
7272
public int maximumDifference(int[] nums) {
73-
int mi = nums[0];
73+
int mi = 1 << 30;
7474
int ans = -1;
75-
for (int i = 1; i < nums.length; ++i) {
76-
if (nums[i] > mi) {
77-
ans = Math.max(ans, nums[i] - mi);
75+
for (int x : nums) {
76+
if (x > mi) {
77+
ans = Math.max(ans, x - mi);
7878
} else {
79-
mi = nums[i];
79+
mi = x;
8080
}
8181
}
8282
return ans;
8383
}
8484
}
8585
```
8686

87+
### **C++**
88+
89+
```cpp
90+
class Solution {
91+
public:
92+
int maximumDifference(vector<int>& nums) {
93+
int mi = 1 << 30;
94+
int ans = -1;
95+
for (int& x : nums) {
96+
if (x > mi) {
97+
ans = max(ans, x - mi);
98+
} else {
99+
mi = x;
100+
}
101+
}
102+
return ans;
103+
}
104+
};
105+
```
106+
107+
### **Go**
108+
109+
```go
110+
func maximumDifference(nums []int) int {
111+
mi := 1 << 30
112+
ans := -1
113+
for _, x := range nums {
114+
if mi < x {
115+
ans = max(ans, x-mi)
116+
} else {
117+
mi = x
118+
}
119+
}
120+
return ans
121+
}
122+
123+
func max(a, b int) int {
124+
if a > b {
125+
return a
126+
}
127+
return b
128+
}
129+
```
130+
87131
### **TypeScript**
88132

89133
```ts
@@ -118,48 +162,27 @@ impl Solution {
118162
}
119163
```
120164

121-
### **C++**
122-
123-
```cpp
124-
class Solution {
125-
public:
126-
int maximumDifference(vector<int>& nums) {
127-
int mi = nums[0];
128-
int ans = -1;
129-
for (int i = 1, n = nums.size(); i < n; ++i) {
130-
if (nums[i] > mi)
131-
ans = max(ans, nums[i] - mi);
132-
else
133-
mi = nums[i];
165+
### **JavaScript**
166+
167+
```js
168+
/**
169+
* @param {number[]} nums
170+
* @return {number}
171+
*/
172+
var maximumDifference = function (nums) {
173+
let mi = 1 << 30;
174+
let ans = -1;
175+
for (const x of nums) {
176+
if (mi < x) {
177+
ans = Math.max(ans, x - mi);
178+
} else {
179+
mi = x;
134180
}
135-
return ans;
136181
}
182+
return ans;
137183
};
138184
```
139185

140-
### **Go**
141-
142-
```go
143-
func maximumDifference(nums []int) int {
144-
mi, ans := nums[0], -1
145-
for i, n := 1, len(nums); i < n; i++ {
146-
if nums[i] > mi {
147-
ans = max(ans, nums[i]-mi)
148-
} else {
149-
mi = nums[i]
150-
}
151-
}
152-
return ans
153-
}
154-
155-
func max(a, b int) int {
156-
if a > b {
157-
return a
158-
}
159-
return b
160-
}
161-
```
162-
163186
### **...**
164187

165188
```

solution/2000-2099/2016.Maximum Difference Between Increasing Elements/Solution.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
class Solution {
22
public:
33
int maximumDifference(vector<int>& nums) {
4-
int mi = nums[0];
4+
int mi = 1 << 30;
55
int ans = -1;
6-
for (int i = 1, n = nums.size(); i < n; ++i) {
7-
if (nums[i] > mi)
8-
ans = max(ans, nums[i] - mi);
9-
else
10-
mi = nums[i];
6+
for (int& x : nums) {
7+
if (x > mi) {
8+
ans = max(ans, x - mi);
9+
} else {
10+
mi = x;
11+
}
1112
}
1213
return ans;
1314
}

solution/2000-2099/2016.Maximum Difference Between Increasing Elements/Solution.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
func maximumDifference(nums []int) int {
2-
mi, ans := nums[0], -1
3-
for i, n := 1, len(nums); i < n; i++ {
4-
if nums[i] > mi {
5-
ans = max(ans, nums[i]-mi)
2+
mi := 1 << 30
3+
ans := -1
4+
for _, x := range nums {
5+
if mi < x {
6+
ans = max(ans, x-mi)
67
} else {
7-
mi = nums[i]
8+
mi = x
89
}
910
}
1011
return ans

solution/2000-2099/2016.Maximum Difference Between Increasing Elements/Solution.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class Solution {
22
public int maximumDifference(int[] nums) {
3-
int mi = nums[0];
3+
int mi = 1 << 30;
44
int ans = -1;
5-
for (int i = 1; i < nums.length; ++i) {
6-
if (nums[i] > mi) {
7-
ans = Math.max(ans, nums[i] - mi);
5+
for (int x : nums) {
6+
if (x > mi) {
7+
ans = Math.max(ans, x - mi);
88
} else {
9-
mi = nums[i];
9+
mi = x;
1010
}
1111
}
1212
return ans;

0 commit comments

Comments
 (0)