leetcode 123. Best Time to Buy and Sell Stock III ----- 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).
之前题的扩展,要求是只能买卖两次,然后求出最大利润。
利用动态规划,从前向后和从后向前各求一次,然后求出结果。
public class Solution {
public int maxProfit(int[] prices) {
int len = prices.length;
if( len < 2 )
return 0;
int[] preProfit = new int[len];
int[] maxProfit = new int[len];
int cur = prices[0];
preProfit[0] = 0;
for( int i = 1;i<len;i++){
cur = Math.min(cur,prices[i]);
preProfit[i] = Math.max(preProfit[i-1],prices[i] - cur);
}
cur = prices[len-1];
maxProfit[len-1] = 0;
for( int i = len-2;i>=0;i--){
cur = Math.max(cur,prices[i]);
maxProfit[i] = Math.max(maxProfit[i+1],cur - prices[i]);
}
int result = 0;
for( int i = 0;i<len;i++){
result = Math.max(preProfit[i]+maxProfit[i],result);
}
return result;
}
}
leetcode 123. Best Time to Buy and Sell Stock III ----- java的更多相关文章
- 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 买卖股票的最佳时间 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 最佳炒股时机之三
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Java for LeetCode 123 Best Time to Buy and Sell Stock III【HARD】
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 (stock problem)
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
原题地址 最直观的想法就是划分成两个子问题,每个子问题变成了:求在某个范围内交易一次的最大利润 在只能交易一次的情况下,如何求一段时间内的最大利润?其实就是找股价最低的一天买进,然后在股价最高的一天卖 ...
- 【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
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
- 【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 ...
随机推荐
- numpy 总结
1.array.sum() from numpy import * import operator group = array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]]) ...
- 使用 JDBC 调用函数 & 存储过程
/** * 如何使用 JDBC 调用存储在数据库中的函数或存储过程 */ @Test public void testCallableStatment() { Connection connectio ...
- 分布式一致性原理—BASE
定义 BASE是BasicallyAvailable(基本可用).Soft state(软状态)和Eventually consistent(最终一致性)三个短语的简写,是由来自eBay的架构师Dan ...
- High Performance Django
构建高性能Django站点 性能 可用 伸缩 扩展 安全 build 1.审慎引入第三方库(是否活跃.是否带入query.是否容易缓存) 2.db:减少query次数 减少耗时query 减小返回 ...
- Android 动画特效
一.渐变动画 AlphaAnimation aa = new AlphaAnimation(0.3f, 1.0f); // fromAlpha , toAlpha aa.setDuration(200 ...
- (kate)win8-64位系统下opencv-2.4.3的安装以及在visual_studio2010中配置
环境: 操作系统:window8.1 64bit Opencv版本:OPencv-2.4.3 VS版本:vs 2010 一.安装Opencv 1.Opencv官网http://opencv.org/ ...
- julia生成指定格式的字符串.jl
julia生成指定格式的字符串.jl """ julia生成指定格式的字符串.jl http://bbs.bathome.net/thread-39829-1-1.htm ...
- form表单验证2
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- powerdesigner的学习
1 powerdesigner的层次 使用过eclipse作为java开发的都知道,workspace是顶级目录,一次只能打开一个,project是项目,位于workspace中,一个workspa ...
- IOS聊天对话界面
大家好,百忙之中,抽出点空,写个微博,话说好久没写. 最近项目中有碰到写类似微信聊天界面上的效果,特整理了一下,写了一个小的Demo,希望给没头绪的同学们一个参考! 下载地址:http://files ...