You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
for (int i = firstIndex; i <S.length() && i <= lastIndex; i++) {
200
-
int index = lastIndexsOfChar[char2Index(S.charAt(i))];
201
-
if (index > lastIndex) {
202
-
lastIndex = index;
203
-
}
204
-
}
205
-
partitions.add(lastIndex - firstIndex +1);
206
-
firstIndex = lastIndex +1;
185
+
publicint maxProfit(int[] prices) {
186
+
int n = prices.length;
187
+
if (n ==0) return0;
188
+
int soFarMin = prices[0];
189
+
int max =0;
190
+
for (int i =1; i < n; i++) {
191
+
if (soFarMin > prices[i]) soFarMin = prices[i];
192
+
else max =Math.max(max, prices[i] - soFarMin);
207
193
}
208
-
returnpartitions;
194
+
returnmax;
209
195
}
196
+
```
210
197
211
-
privateint char2Index(char c) {
212
-
return c -'a';
198
+
199
+
# 5. 买卖股票的最大收益 II
200
+
201
+
[122. Best Time to Buy and Sell Stock II (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/)
202
+
203
+
题目描述:可以进行多次交易,多次交易之间不能交叉进行,可以进行多次交易。
204
+
205
+
对于 [a, b, c, d],如果有 a <= b <= c <= d ,那么最大收益为 d - a。而 d - a = (d - c) + (c - b) + (b - a) ,因此当访问到一个 prices[i] 且 prices[i] - prices[i-1] > 0,那么就把 prices[i] - prices[i-1] 添加到收益中。
206
+
207
+
```java
208
+
publicint maxProfit(int[] prices) {
209
+
int profit =0;
210
+
for (int i =1; i < prices.length; i++) {
211
+
if (prices[i] > prices[i -1]) {
212
+
profit += (prices[i] - prices[i -1]);
213
+
}
214
+
}
215
+
return profit;
213
216
}
214
217
```
215
218
216
219
217
-
# 种植花朵
220
+
# 6. 种植花朵
218
221
219
222
[605. Can Place Flowers (Easy)](https://leetcode.com/problems/can-place-flowers/description/)
@@ -298,27 +301,9 @@ public boolean checkPossibility(int[] nums) {
298
301
}
299
302
```
300
303
301
-
# 股票的最大收益
302
-
303
-
[122. Best Time to Buy and Sell Stock II (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/)
304
304
305
-
题目描述:一次股票交易包含买入和卖出,多个交易之间不能交叉进行。
306
305
307
-
对于 [a, b, c, d],如果有 a <= b <= c <= d ,那么最大收益为 d - a。而 d - a = (d - c) + (c - b) + (b - a) ,因此当访问到一个 prices[i] 且 prices[i] - prices[i-1] > 0,那么就把 prices[i] - prices[i-1] 添加到收益中,从而在局部最优的情况下也保证全局最优。
308
-
309
-
```java
310
-
publicint maxProfit(int[] prices) {
311
-
int profit =0;
312
-
for (int i =1; i < prices.length; i++) {
313
-
if (prices[i] > prices[i -1]) {
314
-
profit += (prices[i] - prices[i -1]);
315
-
}
316
-
}
317
-
return profit;
318
-
}
319
-
```
320
-
321
-
# 子数组最大的和
306
+
# 9. 子数组最大的和
322
307
323
308
[53. Maximum Subarray (Easy)](https://leetcode.com/problems/maximum-subarray/description/)
324
309
@@ -342,28 +327,45 @@ public int maxSubArray(int[] nums) {
342
327
}
343
328
```
344
329
345
-
# 买入和售出股票最大的收益
330
+
# 10. 分隔字符串使同种字符出现在一起
346
331
347
-
[121. Best Time to Buy and Sell Stock (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/)
0 commit comments