LeetCode Notes 007

Best Time to Buy and Sell Stock II

Say you have an array for which the i-th element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

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
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
prices.push(0);

var len = prices.length;
var sum = 0;
var begin;

for (var i = 0; i < len; i++) {
if (i === 0) {
begin = prices[i];
continue;
}

if (prices[i] >= prices[i - 1])
continue;

sum += (prices[i - 1] - begin);
begin = prices[i];
}

return sum;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
if (!prices.length)
return 0;

var buy = []
, sell = [];

prices.forEach(function(item, index) {
if (!index) {
buy[index] = -item;
sell[index] = 0;
} else {
buy[index] = Math.max(buy[index - 1], sell[index - 1] - item);
sell[index] = Math.max(sell[index - 1], buy[index - 1] + item);
}
});

var len = prices.length;
return sell[len - 1];
};

Best Time to Buy and Sell Stock III

Say you have an array for which the i-th element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete at most two transactions.

Note:

You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

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
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
var len = prices.length
, sell = []
, buy = []
, item
, i;

if (!len)
return 0;

for (i = 0; i < len; i++) {
sell[i] = [],
buy[i] = [],
item = prices[i];

if (!i) {
sell[i][0] = 0;
sell[i][1]= 0;
sell[i][2] = 0;
buy[i][0] = -item;
buy[i][1] = -item;
buy[i][2] = -item;
} else {
sell[i][0] = sell[i - 1][0];
sell[i][1] = Math.max(sell[i - 1][1], buy[i - 1][1] + item);
sell[i][2] = Math.max(sell[i - 1][2], buy[i - 1][2] + item);
buy[i][0] = buy[i - 1][0];
buy[i][1] = Math.max(buy[i - 1][1], sell[i - 1][0] - item);
buy[i][2] = Math.max(buy[i - 1][2], sell[i - 1][1] - item);
}
}

return sell[len - 1][2];
};

Best Time to Buy and Sell Stock with Cooldown

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions:

  1. You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
  2. After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)

Example:

1
2
3
prices = [1, 2, 3, 0, 2]
maxProfit = 3
transactions = [buy, sell, cooldown, buy, sell]

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
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
var len = prices.length;

if (len === 0)
return 0;

var sell = []
, buy = []
, rest = [];

for (var i = 0; i < len; i++) {
var item = prices[i];
if (!i) {
sell[i] = 0;
buy[i] = -item;
rest[i] = 0;
} else {
sell[i] = Math.max(sell[i - 1], buy[i - 1] + item);
buy[i] = Math.max(buy[i - 1], rest[i - 1] - item);
rest[i] = Math.max(rest[i - 1], sell[i - 1]);
}
}

return Math.max(sell[len - 1], buy[len - 1], rest[len - 1]);
};
文章作者: Monad Kai
文章链接: onlookerliu.github.io/2018/03/06/LeetCode-Notes-007/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Code@浮生记
支付宝打赏
微信打赏