51
51
52
52
<!-- 这里可写通用的实现逻辑 -->
53
53
54
- 此题并不能单纯遍历数组,寻找其中的最大最小值,然后计算差,因为需要保证最大最小值的前后顺序。
54
+ ** 方法一:维护前缀最小值 **
55
55
56
- ** 步骤: **
56
+ 我们用变量 $mi$ 表示当前遍历到的元素中的最小值,用变量 $ans$ 表示最大差值,初始时 $mi$ 为 $+\infty$,而 $ans$ 为 $-1$。
57
57
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$ 为数组的长度。
61
63
62
64
<!-- tabs:start -->
63
65
68
70
``` python
69
71
class Solution :
70
72
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)
76
78
else :
77
- mi = nums[i]
79
+ mi = x
78
80
return ans
79
81
```
80
82
@@ -85,20 +87,64 @@ class Solution:
85
87
``` java
86
88
class Solution {
87
89
public int maximumDifference (int [] nums ) {
88
- int mi = nums[ 0 ] ;
90
+ int mi = 1 << 30 ;
89
91
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);
93
95
} else {
94
- mi = nums[i] ;
96
+ mi = x ;
95
97
}
96
98
}
97
99
return ans;
98
100
}
99
101
}
100
102
```
101
103
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
+
102
148
### ** TypeScript**
103
149
104
150
``` ts
@@ -133,48 +179,27 @@ impl Solution {
133
179
}
134
180
```
135
181
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;
149
197
}
150
- return ans;
151
198
}
199
+ return ans;
152
200
};
153
201
```
154
202
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
-
178
203
### ** ...**
179
204
180
205
```
0 commit comments