[LeetCOde][Java] 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).
算法分析:
利用通用的算法《Best Time to Buy and Sell Stock
IV》 将k变为2 就ok
AC代码:
<span style="font-family:Microsoft YaHei;font-size:12px;">public class Solution
{
public int maxProfit(int[] prices)
{
return maxProfit(2,prices);
}
public int maxProfit(int k, int[] prices)
{
if(prices==null || prices.length==0)
return 0;
if(k>prices.length)//k次数大于天数时,转化为问题《Best Time to Buy and Sell Stock II》--无限次交易的情景
{
if(prices==null)
return 0;
int res=0;
for(int i=0;i<prices.length-1;i++)
{
int degit=prices[i+1]-prices[i];
if(degit>0)
res+=degit;
}
return res;
}
/*
定义维护量:
global[i][j]:在到达第i天时最多可进行j次交易的最大利润。此为全局最优
local[i][j]:在到达第i天时最多可进行j次交易而且最后一次交易在最后一天卖出的最大利润。此为局部最优
定义递推式:
global[i][j]=max(global[i-1][j],local[i][j]);即第i天没有交易,和第i天有交易
local[i][j]=max(global[i-1][j-1]+max(diff,0),local[i-1][j]+diff) diff=price[i]-price[i-1];
*/
int[][] global=new int[prices.length][k+1];
int[][] local=new int[prices.length][k+1];
for(int i=0;i<prices.length-1;i++)
{
int diff=prices[i+1]-prices[i];
for(int j=0;j<=k-1;j++)
{
local[i+1][j+1]=Math.max(global[i][j]+Math.max(diff,0),local[i][j+1]+diff);
global[i+1][j+1]=Math.max(global[i][j+1],local[i+1][j+1]);
}
}
return global[prices.length-1][k];
}
}</span>
[LeetCOde][Java] Best Time to Buy and Sell Stock III的更多相关文章
- [Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III
Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...
- [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】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]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 ...
- 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][Java] Best Time to Buy and Sell Stock IV
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- 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 ----- java
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- leetcode 【 Best Time to Buy and Sell Stock III 】python 实现
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
随机推荐
- JBoss vs. Tomcat
JBoss 支持Servlet.Web Server和其它J2EE Features Servlet引擎使用Tomcat的内核 SSL性能比Tomcat快4倍(据说) 支持.net.PHP.CGI 支 ...
- 菜鸟nginx源代码剖析数据结构篇(九) 内存池ngx_pool_t
菜鸟nginx源代码剖析数据结构篇(九) 内存池ngx_pool_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csdn ...
- String与StringBuffer的差别
String 是一个常量,即一旦创建不可更改 输出结果为:helloworldjeok 看似.string变量name的值改变了,事实上此name非彼name. 输出结果为: sex:hello wo ...
- 【cocos2d-js官方文档】二十、moduleConfig.json
概述 该配置文件相当于v2版本号中的jsloader.js. 改造的目的是为了使得配置纯粹化,同一时候也能比較好的支持cocos-console.cocos-utils甚至是用户自己定义脚本工具. 字 ...
- MFC 与Excel文件的交互操作
假日快要结束了.带着沉重的心情写下之前关于MFC与Excel文件交互的总结. 因为VS的版本号不同可能在操作上有些差异.所以在此指明下本篇文章的project环境为VS2013,也建议大家用最新的. ...
- 2016.03.10,英语,《Vocabulary Builder》Unit 05
mal: means bad. malpractice [ˌmæl'præktɪs] n. 失职, 行为不当; malady ['mælədi] n. 病, 疾病, 弊病; malodorous [ˌ ...
- 在Kali上安装打印机
在Kali 2.0上安装打印机 最近在玩儿渗透测试,就把自己的办公电脑做成了Kali,可是发现办公室的网络打印机没办法正常使用,上网查了一下,把整个过程简单的记录一下,省的忘记了 1.安装cups a ...
- weblogic状态监控脚本
echo "======================================welcome============================================ ...
- Extjs Grid 各种Demo
grid的一个渲染效果 Ext.define('cfWeb.view.accountSetting.OrgManageView',{ alias : 'widget.orgManageView', e ...
- POJ 2665 模拟,,
It is confirmed that these sections do not overlap with each other. 一句话 就变成水题了,,, // by SiriusRen #i ...