题目:

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. Win+X

    Win+X 方便的快捷键,可以快速使用命令行和运行!

  2. 8.MATLAB数据分析

    概述: clc; clear all; p1=[ ]; y=poly2sym(p1) %由向量创建多项式 disp(y) %显示多项式 1 多项式的求值与求根 clc; clear all; p=[ ...

  3. 3d touch 的使用(一)

    废话不多说,直接上代码------------------ 在 - (BOOL)application:(UIApplication *)application didFinishLaunchingW ...

  4. 关于ubuntu中文输入调用不出来的解决办法,具体如正文。

    卸载了 fcitx sudo apt-get remove fcitx 重启 sudo reboot 重新安装 fcitxsudo apt-get install fcitx 安装拼音输入法sudo ...

  5. Android Fragment RecycleListView

    1.新建SuperActivity package com.example.ting.criminalintentpractise; import android.os.Bundle;import a ...

  6. Spark SQL概念学习系列之性能调优

    不多说,直接上干货! 性能调优 Caching Data In Memory Spark SQL可以通过调用sqlContext.cacheTable("tableName") 或 ...

  7. http协议以及防盗链技术

    http协议,又称为超文本传输协议,顾名思义,http协议不仅能传输文本,还能传输图片,视频,压缩包等文件,http协议是建立在tcp/ip协议的基础之上的,http协议对php程序员来讲可以说是重中 ...

  8. WCF(二)配置文件

    上篇文章中对WCF的配置放到App.config中,这样可以使程序更灵活.更具有扩展性. 下面说下配置文件中各个节点的含义. 服务端: WCF配置文件节点放在<system.serviceMod ...

  9. js正则获取html字符串指定的dom元素和内容

    var str = "<div>111<p id='abc'>3333</p></div><div>222<div id=' ...

  10. soapUI检查webServices接口的方法以及对自动触发线程的查询

    这几天需要熟悉接口传输过来的数据,因此会用到soapUI,但是没结果这个工具,然后百度了下,结合了下,下面是我对webservice在soapUI的展现: 1:其实说白了,就是我们不知道从接口里传输过 ...