123. Best Time to Buy and Sell Stock III
来源:互联网 发布:淘宝生活网 编辑:程序博客网 时间:2024/06/05 02:32
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 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).
Solution 1
public static int maxProfit2(int[] prices) {if (prices == null || prices.length == 0) return 0; int lenght = prices.length; int[] leftProfit = new int[lenght]; int leftMaxProfit = 0; int leftMin = prices[0]; for (int i=0; i<lenght; i++) { if (prices[i] < leftMin) leftMin = prices[i]; if (prices[i] - leftMin > leftMaxProfit) leftMaxProfit = prices[i]-leftMin; leftProfit[i] = leftMaxProfit; } int maxProfit = 0; int rightMaxProfit = 0; int rightMax = prices[lenght-1]; for (int i=lenght-1; i>=0; i--) { if (prices[i] > rightMax) rightMax = prices[i]; if (rightMax - prices[i] > rightMaxProfit) rightMaxProfit = rightMax - prices[i]; int currentProfit = rightMaxProfit + (i>0 ? leftProfit[i-1] : 0); if (currentProfit > maxProfit) { maxProfit = currentProfit; } } return maxProfit;}
public static int maxProfit3(int[] prices) { if(prices == null || prices.length == 0) return 0; int[] dp = new int[prices.length];//k == 0, dp[i] = 0 int K = 2, tmpMax = 0; for(int k =1; k<=K; k++) { tmpMax = dp[0] - prices[0]; dp[0] = 0; for(int i = 1; i<prices.length; i++) { tmpMax = Math.max(tmpMax, dp[i] - prices[i]); dp[i] = Math.max(dp[i-1], prices[i]+tmpMax); } } return dp[prices.length -1];}
Best solution O(N)
First assume that we have no money, so buy1 means that we have to borrow money from others, we want to borrow less so that we have to make our balance as max as we can(because this is negative).sell1 means we decide to sell the stock, after selling it we have price[i] money and we have to give back the money we owed, so we have price[i] - |buy1| = prices[i ] + buy1, we want to make this max.
buy2 means we want to buy another stock, we already have sell1 money, so after buying stock2 we have buy2 = sell1 - price[i] money left, we want more money left, so we make it max
sell2 means we want to sell stock2, we can have price[i] money after selling it, and we have buy2 money left before, so sell2 = buy2 + prices[i], we make this max.
public int maxProfit(int[] prices) { int sell1 = 0, sell2 = 0, buy1 = Integer.MIN_VALUE, buy2 = Integer.MIN_VALUE; for(int i = 0; i < prices.length; i++){ buy1 = Math.max(buy1, -prices[i]); sell1 = Math.max(sell1, buy1 + prices[i]); buy2 = Math.max(buy2, sell1 - prices[i]); sell2 = Math.max(sell2, buy2 + prices[i]); } return sell2; }
0 0
- LeetCode 123. Best Time to Buy and Sell Stock III
- 123.Best Time to Buy and Sell Stock III
- [Leetcode]123.Best Time to Buy and Sell Stock III
- [Leetcode] 123. Best Time to Buy and Sell Stock III
- 123. Best Time to Buy and Sell Stock III
- [leetcode] 123.Best Time to Buy and Sell Stock III
- 【leetcode】123. Best Time to Buy and Sell Stock III
- 123. Best Time to Buy and Sell Stock III
- [leetcode] 123. Best Time to Buy and Sell Stock III
- 123. Best Time to Buy and Sell Stock III
- 123. Best Time to Buy and Sell Stock III
- leetcode 123. Best Time to Buy and Sell Stock III
- leetcode 123. Best Time to Buy and Sell Stock III
- 123. Best Time to Buy and Sell Stock III
- 123. Best Time to Buy and Sell Stock III
- [LeetCode] 123. Best Time to Buy and Sell Stock III
- 123. Best Time to Buy and Sell Stock III
- Leetcode 123. Best Time to Buy and Sell Stock III
- Android中定时器Timer和TimerTask的启动,停止,暂停,继续等操作实例
- vbscript对文件夹遍历
- Windows下Eclipse for C/C++的“Launch failed. Binary not found”完美解决方案
- Android基础-TextView用法
- 深搜算法:倒油/面向对象的思想来做
- 123. Best Time to Buy and Sell Stock III
- 快慢指针判断单向链表是否有环及找环入口
- C++ Exceptional 类的重载、重写(覆盖)和隐藏
- [leetcode] 256. Paint House 解题报告
- ef生成codefirst上下版本之间的迁移记录
- Hibernate-Preface(Hibernate前言)
- 实现计时器的几种方式
- [leetcode] 280. Wiggle Sort 解题报告
- 看最强大脑中英PK小记