题目:

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. 低调、奢华、有内涵的敏捷式大数据方案:Flume+Cassandra+Presto+SpagoBI

    基于FacebookPresto+Cassandra的敏捷式大数据 文件夹 1 1.1 1.1.1 1.1.2 1.2 1.2.1 1.2.2 2 2.1 2.2 2.3 2.4 2.5 2.6 3 ...

  2. UVALive 6663 Count the Regions 离散+bfs染色_(:зゝ∠)_

    题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=4675">点击打开链接 gg.. ...

  3. 2014年湖北省TI杯大学生电子设计竞赛论文格式

    2014年湖北省TI杯大学生电子设计竞赛 B题:金属物体探測定位器(本科) 2014年8月15日 文件夹 1 系统方案 1.1 XXX的论证与选择........................... ...

  4. 怎样避免使用Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK之后的黑屏问题

    在自己的项目中.我须要使用Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK来開始新的activity同一时候移除之前全部的 ...

  5. Ruby学习(三)——类与对象(1)

    今天看了<Ruby元编程>,感觉内容新颖翔实,是Ruby中难得的一见的好书,在此推荐给大家.其实今天看的主要是第一章的第一部分,先把内容梳理一下,也许这一部分会分成几天的内容来给大家介绍吧 ...

  6. 英语发音规则---K字母

    英语发音规则---K字母 一.总结 一句话总结: 1.K发[k]音? kind [kaɪnd] n. 种类 bike [baɪk] n. 自行车 skate [skeɪt] vi. 滑冰 make [ ...

  7. CSS中alt和title属性的正确使用

    1.在<img>标签中的使用 alt:全称为alttext,实质是当图片无法正确显示时用于替换(在IE下同时起到了title的作用,即鼠标滑过时文字提示): title:鼠标经过时文字提示 ...

  8. c++ set_union set_intersection使用

    自定义类型也可以构造set,但同样必须定义“小于”运算符,set中的元素从小到大排列好了 #include<iostream>#include<string>#include ...

  9. 构建工具系列一--Travis-cli

    本文地址: http://www.cnblogs.com/blackmanba/articles/continuous-integration-tool-travis-cli.html或者http:/ ...

  10. Vue运行npm run dev 时修改端口

    进入项目文件的config文件夹E:\myapp\myproject\config,找到index.js,修改里面的8080端口,改成8088(确定不被别的程序使用的都可以)