您的位置:首页 > 其它

Best Time to Buy and Sell Stock系列

2016-03-24 11:57 281 查看
I题

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

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Subscribe to see which companies asked this question

解答:采用动态规划解法,遍历数组更新当前最小值,并用当前值减去最小值与当前最大利润进行比较,更新最大利润。

public class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0) {
return 0;
}
int min = Integer.MAX_VALUE;
int profit = 0;
for (int i : prices) {
min = Math.min(min, i);
profit = Math.max(i - min, profit);
}
return profit;
}
}


II

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). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

Subscribe to see which companies asked this question

解答:采用贪心算法,记录相邻两天的差值,大于零则加到利润总量中。(本题满足贪心算法可以得到最优解的特性是重点)

public class Solution {
public int maxProfit(int[] prices) {
int profit = 0;
for (int i = 0; i < prices.length - 1; i++) {
int pro = prices[i + 1] - prices[i];
if (pro > 0) {
profit += pro;
}
}
return profit;
}
}


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:

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

After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)

Example:

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

解答:设置三个数组,buy sell和rest。

buy[i]表示第i天以buy状态结束时至今可以获得的最大profit;

sell[i]表示第i天以sell状态结束至今可以获得的最大profit;

rest[i]表示第i天以rest状态结束至今可以获得的最大profit。

根据定义可得:

buy[i]  = max(rest[i-1]-price, buy[i-1]) //前者表示在前一天rest的情况下今天购入,后者表示在前一天处于buy状态的情况下保持buy状态(即不操作)
sell[i] = max(buy[i-1]+price, sell[i-1]) //前者表示在前一天处于buy状态的情况下今天卖出,后者表示前一天已经处于sell状态的情况下保持sell状态(即不操作)
rest[i] = max(sell[i-1], buy[i-1], rest[i-1]) //rest表示没有任何操作,所以其值应为前一天处于sell,buy和rest状态中的最大值


上述等式考虑了两个规则,即buy必须在rest之后,sell必须在buy之后,由一式和二式可以得到。但还有一个特殊情况[buy,rest,buy]需要考虑,从上述三式不能明显得出这种特殊情况不存在。

由buy[i] <= rest[i],故rest[i] = max(sell[i-1], rest[i-1]),由此式可以得到[buy,rest,buy]不可能发生。

由rest[i] <= sell[i],故rest[i] = sell[i-1]。

于是上述三个等式可以化为两个:

buy[i] = max(sell[i-2]-price, buy[i-1])
sell[i] = max(buy[i-1]+price, sell[i-1])


第i天状态只和第i-1天和i-2天有关,所以空间可以由O(n)缩小为O(1)。

具体代码如下:

public int maxProfit(int[] prices) {
int sell = 0, prev_sell = 0, buy = Integer.MIN_VALUE, prev_buy;
for (int price : prices) {
prev_buy = buy;
buy = Math.max(prev_sell - price, prev_buy);
prev_sell = sell;
sell = Math.max(prev_buy + price, prev_sell);
}
return sell;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: