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 ...
随机推荐
- SQL_异化
select a.pk_accasoa from bd_accasoa a; --下级科目原来主键: 0001Z0100000000001A2 --执行该语句后下级科目异化了(替换的意思) , '@@ ...
- Tensorflow学习:(三)神经网络优化
一.完善常用概念和细节 1.神经元模型: 之前的神经元结构都采用线上的权重w直接乘以输入数据x,用数学表达式即,但这样的结构不够完善. 完善的结构需要加上偏置,并加上激励函数.用数学公式表示为:.其中 ...
- 第1天:Django框架简介与工程创建
Django简介 Django特点 环境搭建 创建工程 启动服务 使用pycharm打开工程 创建子应用 Django简介 Django,是用Python语言写的开源web开发框架,并遵循MVC设计. ...
- shell 规范
代码风格规范 开头有“蛇棒” 所谓shebang其实就是在很多脚本的第一行出现的以”#!”开头的注释,他指明了当我们没有指定解释器的时候默认的解释器,一般可能是下面这样: #!/bin/bash 当然 ...
- 拉格朗日乘子法以及KKT条件
拉格朗日乘子法是一种优化算法,主要用来解决约束优化问题.他的主要思想是通过引入拉格朗日乘子来将含有n个变量和k个约束条件的约束优化问题转化为含有n+k个变量的无约束优化问题. 其中,利用拉格朗日乘子法 ...
- 决策树算法(ID3)
Day Outlook Temperature Humidity Wind PlayTennis 1 Sunny Hot High Weak No 2 Sunny Hot High Strong No ...
- 轻巧的编辑器:Sublime Text3 user设置
开发到现在,编辑器倒用过不少,VIM.zend.my eclipse.EPP.editplus.notepad++.sublime text 2. 最初使用sublime是同学推荐的,说其何其的好,何 ...
- java并发基础(一)
最近在看<java并发编程实战>,希望自己有毅力把它读完. 线程本身有很多优势,比如可以发挥多处理器的强大能力.建模更加简单.简化异步事件的处理.使用户界面的相应更加灵敏,但是更多的需要程 ...
- 邮件基本常识普及(to/cc/bcc)
http://blog.sina.com.cn/s/blog_5572b4b5010009ul.html 前两天,某个同事发的一封邮件着实把我给郁闷了,他发的是图片形式的笑话,内容稍稍有点不太健康,这 ...
- strace_for_windows
http://drmemory.org/strace_for_windows.html https://github.com/intellectualheaven/stracent/releases