Best Time to Buy and Sell Stock III leetcode java
题目:
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).
题解:
根据题目要求,最多进行两次买卖股票,而且手中不能有2只股票,就是不能连续两次买入操作。
所以,两次交易必须是分布在2各区间内,也就是动作为:买入卖出,买入卖出。
进而,我们可以划分为2个区间[0,i]和[i,len-1],i可以取0~len-1。
那么两次买卖的最大利润为:在两个区间的最大利益和的最大利润。
一次划分的最大利益为:Profit[i] = MaxProfit(区间[0,i]) + MaxProfit(区间[i,len-1]);
最终的最大利润为:MaxProfit(Profit[0], Profit[1], Profit[2], ... , Profit[len-1])。
代码如下:
1 public int maxProfit(int[] prices) {
2 if(prices == null || prices.length <= 1){
3 return 0;
4 }
5 int len = prices.length;
6 int maxProfit = 0;
7 int min = prices[0];
8 int arrayA[] = new int[len];
9
for(int i=1;i<prices.length;i++){
min=Math.min(min,prices[i]);
arrayA[i]=Math.max(arrayA[i-1],prices[i]-min);
}
int max = prices[len-1];
int arrayB[] = new int[len];
for(int i = len-2; i >= 0; i--){
max = Math.max(prices[i],max);
arrayB[i] = Math.max(max-prices[i],arrayB[i+1]);
}
for(int i = 0; i < len; i++){
maxProfit = Math.max(maxProfit,arrayA[i] + arrayB[i]);
}
return maxProfit;
}
Reference:
http://blog.csdn.net/u013027996/article/details/19414967
Best Time to Buy and Sell Stock III leetcode java的更多相关文章
- 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 ...
- 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 ...
- Best Time to Buy and Sell Stock II leetcode java
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...
- LeetCode 笔记23 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 ...
- Best Time to Buy and Sell Stock | & || & III
Best Time to Buy and Sell Stock I Say you have an array for which the ith element is the price of a ...
- 【leetcode】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 ...
- 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 al ...
- 【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 ...
随机推荐
- @ControllerAdvice + @ExceptionHandler 处理 全部Controller层异常
对于与数据库相关的 Spring MVC 项目,我们通常会把 事务 配置在 Service层,当数据库操作失败时让 Service 层抛出运行时异常,Spring 事物管理器就会进行回滚. 如此一来, ...
- Linux-UDP-Socket编程
接收CAN总线上的数据并将其发送出去 创建客户端: /******************************************************************** * co ...
- Android调用C#的WebService
Android调用C#写的WebService 学习自: http://www.cnblogs.com/kissazi2/p/3406662.html 运行环境 Win10 VS 2015 Andro ...
- Windows 安装 Jenkins 2.6
最近都是Windows下干活啊... 一.下载和安装 官网地址:https://jenkins.io/index.html,选择了2.X系列的Windows版本,自动在浏览器下载到了jenkins-2 ...
- 【BZOJ-3110】K大数查询 整体二分 + 线段树
3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 6265 Solved: 2060[Submit][Sta ...
- UOJ #17. 【NOIP2014】飞扬的小鸟 背包DP
#17. [NOIP2014]飞扬的小鸟 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4902 Solved: 1879 题目连接 http:// ...
- 如何添加mysql到环境变量
环境: 在自己安装的lampp环境下,当使用mysql的时候必须指定路径才能进入数据库:这样显得太过麻烦.我们可以通过将mysql加入到环境变量中来解决该问题(mysql执行路径/opt/lampp/ ...
- linux基础环境部署
Content 0.序 1.更新安装库 2.安装基础库 0.序 本文主要是记录php在 Centos下的安装配置 .文中如无特别说明.表示php-5.6.31代码目录. 1.更新安装库 $ yum u ...
- Slickflow.NET 开源工作流引擎基础介绍(二) -- 引擎组件和业务系统的集成
集成流程引擎的必要性 业务过程的变化是在BPM系统中常见的现象,企业管理层需要不断优化组织架构,改造业务流程,不可避免地带来了业务流程的变化,企业信息系统就会随之面临重构的可能性.一种直接的方式是改造 ...
- Git_Feature分支
软件开发中,总有无穷无尽的新的功能要不断添加进来. 添加一个新功能时,你肯定不希望因为一些实验性质的代码,把主分支搞乱了,所以,每添加一个新功能,最好新建一个feature分支,在上面开发,完成后,合 ...