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
解答:采用动态规划解法,遍历数组更新当前最小值,并用当前值减去最小值与当前最大利润进行比较,更新最大利润。
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
解答:采用贪心算法,记录相邻两天的差值,大于零则加到利润总量中。(本题满足贪心算法可以得到最优解的特性是重点)
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:
解答:设置三个数组,buy sell和rest。
buy[i]表示第i天以buy状态结束时至今可以获得的最大profit;
sell[i]表示第i天以sell状态结束至今可以获得的最大profit;
rest[i]表示第i天以rest状态结束至今可以获得的最大profit。
根据定义可得:
上述等式考虑了两个规则,即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]。
于是上述三个等式可以化为两个:
第i天状态只和第i-1天和i-2天有关,所以空间可以由O(n)缩小为O(1)。
具体代码如下:
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; }
相关文章推荐
- 为什么Android的点9图四周的颜色只能是白、黑或透明
- 自定义DialogFrament弹框方式(底部,居中等)
- IOS中UITableViewCell使用详解
- Volley+OkHttp的整合
- error opening trace file: No such file or directory (2)
- error opening trace file: No such file or directory (2)
- Windows 7如何建立一个FTP的快捷方式
- Android EditText输入最大长度限制如何给用户以友好的提示
- Servlet详解
- js function 调用时 new一个还是直接调用
- Limitations of Forms Personalization (文档 ID 420518.1)
- 第三周总结
- app提交版本更新的流程
- WDS
- hive报错整理之Malformed ORC file 、Invalid postscript.
- CircleProgress
- javarebel配置
- 在生产环境中使用Apache Mesos和Docker
- SQL server清空数据库日志脚本
- Edittext设置输入属性,包括使用代码设置