-
Notifications
You must be signed in to change notification settings - Fork 70
/
res.js
42 lines (34 loc) · 750 Bytes
/
res.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
let min = 999999, max = -1;
let res = 0;
for (let index = 0; index < prices.length; index++) {
const element = prices[index];
if (min > element) {
min = element;
max = -1;
} else if (max < element) {
max = element;
}
if (min !== 999999 && max !== -1) {
res = Math.max(res, max - min);
}
}
return res;
};
/**
* DP 解法
* @param {*} prices
*/
var maxProfit_2 = (prices) => {
if (prices.length < 2) return 0;
let last = 0, max = 0;
for (let i = 1; i < prices.length; i++) {
last = Math.max(0, last + prices[i] - prices[i-1]);
max = Math.max(last, max);
}
return max;
}