Best Time to Buy and Sell Stock III
来源:互联网 发布:天融信数据库防火墙 编辑:程序博客网 时间:2024/04/27 17:12
题目原型:
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).
基本思路:
只能交易两次。
找寻一个点j,将原来的price[0..n-1]分割为price[0..j]和price[j..n-1],分别求两段的最大profit。为什么是[0..j]和[j..n-1]而
不是[0..j]和[j+1..n-1],这里是防止出现1,2,3,4,5,6的情况最大值是5而不是4,也就是说一次交易,在1的时候买入,在
6的时候卖出。而当数组不是一直递增的 情况下,这两种写法是一样的。
进行优化:
对于点j+1,求price[0..j+1]的最大profit时,很多工作是重复的,在求price[0..j]的最大profit中已经做过了。
类似于Best Time to Buy and Sell Stock,可以在O(1)的时间从price[0..j]推出price[0..j+1]的最大profit。
但是如何从price[j..n-1]推出price[j+1..n-1]?反过来思考,我们可以用O(1)的时间由price[j+1..n-1]推出price[j..n-1]。
最终算法:
数组l[i]记录了price[0..i]的最大profit,
数组r[i]记录了price[i..n]的最大profit。
已知l[i],求l[i+1]是简单的,同样已知r[i],求r[i-1]也很容易。
最后,我们再用O(n)的时间找出最大的l[i]+r[i],即为题目所求。
// 基本思想是分成两个时间段,然后对于某一天,计算之前的最大值和之后的最大值public int maxProfit(int[] prices){if (prices.length == 0){return 0;}int max = 0;// dp数组保存左边和右边的利润最大值int[] left = new int[prices.length]; // 计算[0,i]区间的最大值int[] right = new int[prices.length]; // 计算[i,len-1]区间的最大值process(prices, left, right);// O(n)找到最大值for (int i = 0; i < prices.length; i++){max = Math.max(max, left[i] + right[i]);}return max;}public static void process(int[] prices, int[] left, int[] right){left[0] = 0;int min = prices[0];// 左边递推公式for (int i = 1; i < left.length; i++){left[i] = left[i - 1] > prices[i] - min ? left[i - 1] : prices[i]- min;min = prices[i] < min ? prices[i] : min;}right[right.length - 1] = 0;int max = prices[right.length - 1];// 右边递推公式for (int i = right.length - 2; i >= 0; i--){right[i] = right[i + 1] > max - prices[i] ? right[i + 1] : max- prices[i];max = prices[i] > max ? prices[i] : max;}}
- LeetCode Best Time to Buy and Sell Stock III
- LeetCode Best Time to Buy and Sell Stock III
- [Leetcode] Best Time to Buy and Sell Stock III
- leetcode 16: Best Time to Buy and Sell Stock III
- LeetCode Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock III
- [LeetCode] Best Time to Buy and Sell Stock III
- leetcode 62: Best Time to Buy and Sell Stock III
- [LeetCode] Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock III
- 【leetcode】Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock III
- LeetCode - Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock III
- LeetCode之Best Time to Buy and Sell Stock III
- [LeetCode]Best Time to Buy and Sell Stock III
- Mac&IOS Socket编程
- 配置SecureCRT的颜色与字体方案
- (android)JSONObject与JSONArray的使用
- 每个程序员都应该了解的内存知识
- Excel专业图表设计系列(一):复选框控制图表展现
- Best Time to Buy and Sell Stock III
- Samung LCD接口原理
- 情花三弄,友声三重
- 特征变换(2)沃尔什-哈达玛变换
- 启动activity,自定义action,action常量大全
- Xcode配置SVN详细步骤
- win7 route 命令 重装TCP/IP 协议
- 每天进步一点点- lsof 命令查找指定用户、进程、端口打开的文件
- hdu 2037 贪心+结构体排序(备战LQB)