LeerCode 123 Best Time to Buy and Sell Stock III之O(n)解法
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).
才意识到能够在整个区间的每一点切开,然后分别计算左子区间和右子区间的最大值,然后再用O(n)时间找到整个区间的最大值。
package Level4; import java.util.Arrays; /**
* Best Time to Buy and Sell Stock III
*
* 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).
*
*/
public class S123 { public static void main(String[] args) {
// int[] prices = {3,3,5,0,0,3,1,4};
int[] prices = {2,1,2,0,1};
System.out.println(maxProfit(prices));
} // 基本思想是分成两个时间段,然后对于某一天,计算之前的最大值和之后的最大值
public static 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;
} // System.out.println(Arrays.toString(left));
// System.out.println(Arrays.toString(right));
} }
LeerCode 123 Best Time to Buy and Sell Stock III之O(n)解法的更多相关文章
- LN : leetcode 123 Best Time to Buy and Sell Stock III
lc 123 Best Time to Buy and Sell Stock III 123 Best Time to Buy and Sell Stock III Say you have an a ...
- 【leetcode】123. Best Time to Buy and Sell Stock III
@requires_authorization @author johnsondu @create_time 2015.7.22 19:04 @url [Best Time to Buy and Se ...
- [leetcode]123. Best Time to Buy and Sell Stock III 最佳炒股时机之三
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【刷题-LeetCode】123 Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
- 123. Best Time to Buy and Sell Stock III
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- LeetCode OJ 123. Best Time to Buy and Sell Stock III
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 123. Best Time to Buy and Sell Stock III ——LeetCode
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 123. Best Time to Buy and Sell Stock III (Array; DP)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- 使用KnockoutJs+Bootstrap实现分页
[后端人员耍前端系列]KnockoutJs篇:使用KnockoutJs+Bootstrap实现分页 一.引言 由于最近公司的系统需要改版,改版的新系统我打算使用KnockoutJs来制作Web前端 ...
- zoj 2822 Sum of Different Primes (01背包)
///给你n 求他能分解成多少个的不同的k个素数相加之和 ///01背包,素数打表 # include <stdio.h> # include <algorithm> # in ...
- 在VM已安装Android4.4 连接小米手环 网络设置
1.打开一个终端 2.输入su,选择同意 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGl5dW4xMjNneA==/font/5a6L5L2T/fon ...
- sql server数据库保存图片或者其他小文件
原文:sql server数据库保存图片或者其他小文件 测试用sql server数据库保存图片或者其他小文件. 文件流字段用varbinary类型. static void Main() { App ...
- [Java聊天室server]实战之五 读写循环(服务端)
前言 学习不论什么一个稍有难度的技术,要对其有充分理性的分析,之后果断做出决定---->也就是人们常说的"多谋善断":本系列尽管涉及的是socket相关的知识,但学习之前,更 ...
- Linux中进行挂起(待机)的命令说明
/********************************************************************* * Author : Samson * Date ...
- sql优化-提防错误关联
在写sql时,在多表关联时,有时候容易把关联关系写错.一般情况下,该问题比较容易发现,但如果sql较长时,光靠眼力就比较难发现了.今天写了一个脚本,碰到该问题了. 第一版本的脚本如下: select ...
- poj1185(状压dp)
题目连接:http://poj.org/problem?id=1185 题意:给出一张n*m的地图,'H'表示高地,不能部署炮兵,'P'表示平原,可以部署炮兵,炮兵之间必须保持横向.纵向至少2个格子的 ...
- fzu2150(bfs)
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150 题意:在任意两处点火,求最短时间烧光所有草堆. 分析:由于n,m比较小,将所有草堆坐标记录下来,然后暴力 ...
- SE 2014年4月13日
要求自治系统之间建立BGP邻居关系,AS 100 中由于配置疏忽R5上忘记启用BGP,从而导致了黑洞问题出现.从而需要网络工程师们就现状问题进行分析,并且使用相应技术进行完善,使得AS 400 和AS ...