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 ...
随机推荐
- Linux-设置环境变量
一般来说,配置交叉编译工具链的时候需要指定编译工具的路径,此时就需要设置环境变量.例如我的mips-linux-gcc编译器在“ /opt/au1200_rm/build_tools/bin”目录下, ...
- 推荐一款好用轻便的在线UML画图工具
刚接触UML时间不长,看了N多教学视频,下载好了几个软件各种不习惯 当我遇见了ProcessOn 从此我彻底“爱上”了它! http://www.processon.com/ UML各类例图它几乎全 ...
- xlistview的XML(头)xlistview_header
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" ...
- S5PV210之添加缺少的-内核提供的'.h'文件 linux3.0.8驱动
怎样解决编译时出现内核提供的函数或变量没有定义,使用source insight搜索功能找到声明的头文件,然后包含该头件就行了: 比如: error: implicit declaration of ...
- 后台获取不规则排列RadioButton组的值
获取多个RadioButton的值,我们一般会使用服务器控件RadioButtonList: <asp:RadioButtonList ID="rbl" runat=&quo ...
- char、nvarchar和varchar区别
这3种字符串数据类型是我们使用最多的,我们在数据库设计时到底该怎么使用了?首先我们先来分析3个数据类型的说明: 1.char CHAR的长度是固定的,最长2000个字符. 2.varchar 和 va ...
- 如何从SAP中查找BADI
如何从SAP中查找BADI 如何从SAP中查找BADI http://blog.csdn.net/CompassButton/article/details/1231652 BADI作为SAP的第 ...
- C#导入EXCEL数据
public static void InputUserFromExcel(string filePath) { string FileExName = filePath.Substring(file ...
- linux命令:cp
1.命令介绍: cp用来复制文件或目录,全称是copy 2.命令格式: cp [选项] [-T] 源 目的 或cp [选项] 源 目录 或cp [选项] -t 目录 源 3.命令参数: -a, --a ...
- 【LeetCode OJ】Evaluate Reverse Polish Notation
Problem link: http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ According to the wik ...