题目:

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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 【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 ...

  4. [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 ...

  5. 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 ...

  6. [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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. UVA 294 - Divisors 因子个数

    Mathematicians love all sorts of odd properties of numbers. For instance, they consider 945 to be an ...

  2. C语言的长处和缺点

     C语言的长处和缺点 C语言的长处: 1.面向过程的语言C语言是面向过程的语言,在这里用户创建过程或函数来运行他们的任务. 面向过程的语言是非常easy学.因为它遵循的算法来运行你的语句.要使用面 ...

  3. intelliJ idea运行新的test功能时,报错:class not found "....." empty test suite

    转自:https://blog.csdn.net/u012560212/article/details/75037578

  4. 方括号在sqlserver中的作用

    我也很无奈啊... 竟然还有这个技能...建个表试试? 显然不行... 再检查下表结构吧 前方小前辈有点坑... selelct [group] from tablle   可以了√ 应该是把[方括号 ...

  5. 深度理解DOM事件(实例)

    前言 通过如下两个实例来理解DOM事件 实例1--点击别处关闭浮层 onclick与addEventListener的区别 实例2--点击后颜色一层一个层出现的漂亮的彩虹圈 1  实例1--点击别处关 ...

  6. js通过经纬度计算两点之间的距离

    最近这几天在做地图的时候,获取到目的地经纬度和当前所在位置的经纬度,通过这几个参数,用js代码就能获取到这两点之间的直线距离: function (lat1, lng1, lat2, lng2) { ...

  7. DOM基础知识(Node对象、Element对象)

    5.Node对象 u  遍历节点 u 父节点 .parentNode - 获取父节点—> 元素节点或文档节点 .parentElement - 获取父元素节点—> 元素节点 u    子节 ...

  8. ZBrush 笔刷的基础参数

    ZBrush®中的笔刷基本参数主要包括3个:Draw Size(绘制大小).Focal Shift(焦点衰减)和Z Intensity(深度强度),通常使用这3个基本参数对笔刷进行调整. 在视图文档区 ...

  9. 2、Attentive Group Recommendation----注意力集中的群组推荐

    1.摘要: 采用attention和NCF结合解决群组偏好融合的问题. 贡献: 第一个使用神经网络学习融合策略的组推荐. 进一步整合用户-项目交互改进组推荐,减轻冷启动问题. 2.方法: 模型AGRE ...

  10. [置顶] 智能家居开源项目 The open Home Automation Bus (openHAB)

    ================================================================================ 2014-05-19 论文的事情太多, ...