题目:

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

Note:

You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

题意:

给定一个数组,数组中第 i 个元素,表示给定的一仅仅股票在第 i 天的价格.

设计一个算法找出最大的收益。

你最多被同意
k 次交易。

注意:

同一时间你不能进行多次交易。

(即:在你必须先卖掉这仅仅股票才干再次购买)

算法分析:

採用动态规划进行求解,使用局部最优和全局最优解法

因为要考虑交易次数。维护量应该就是一个二维数组。

定义维护量:

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];

就是看两个量。第一个是全局到i-1天进行j-1次交易,然后加上今天的交易。假设今天是赚钱的话(也就是前面仅仅要j-1次交易。最后一次交易取当前天),第

二个量则是取local第i-1天j次交易,然后加上今天的差值(这里由于local[i-1][j]比方包括第i-1天卖出的交易,所以如今变成第i天卖出。并不会添加交易次数,

并且这里不管diff是不是大于0都一定要加上。由于否则就不满足local[i][j]必须在最后一天卖出的条件了)

这道题还有个坑,就是假设k的值远大于prices的天数。比方k是好几百万,而prices的天数就为若干天的话,上面的DP解法就很的没有效率,应该直接用

Best
Time to Buy and Sell Stock II
》的方法来求解。所以实际上这道题是之前的二和三的综合体。

AC代码:

<span style="font-family:Microsoft YaHei;font-size:12px;">public class Solution
{
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 IV的更多相关文章

  1. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

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

  3. [LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  4. 【LeetCode】Best Time to Buy and Sell Stock IV

    Best Time to Buy and Sell Stock IV Say you have an array for which the ith element is the price of a ...

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

  6. LeetCode 188. Best Time to Buy and Sell Stock IV (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 ...

  7. [LeetCode] 121. Best Time to Buy and Sell Stock 买卖股票的最佳时间

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  8. [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II

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

随机推荐

  1. [python学习篇][书籍学习][python standrad library][内建类型]迭代器类型

    我们已经知道,可以直接作用于for循环的数据类型有以下几种:一类是集合数据类型,如list.tuple.dict.set.str等:一类是generator,包括生成器和带yield的generato ...

  2. Codeforces Round #410 (Div. 2) A. Mike and palindrome

    A. Mike and palindrome time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  3. 【bzoj2467】[中山市选2010]生成树 矩阵树定理

    题目描述 有一种图形叫做五角形圈.一个五角形圈的中心有1个由n个顶点和n条边组成的圈.在中心的这个n边圈的每一条边同时也是某一个五角形的一条边,一共有n个不同的五角形.这些五角形只在五角形圈的中心的圈 ...

  4. 备忘 CSS字体中英文名称对照表

    转载自:http://www.jb51.net/css/67658.html 在CSS文件中,我们常看到有些字体名称变成了乱码,这是由于编写者将中文字体的名字直接写成了中文,并且再上传或者拷贝复制的时 ...

  5. [USACO07NOV]牛继电器Cow Relays (最短路,DP)

    题目链接 Solution 非正解 似乎比较蛇啊,先个一个部分分做法,最短路+\(DP\). 在求最短路的堆或者队列中存储元素 \(dis_{i,j}\) 代表 \(i\) 这个节点,走了 \(j\) ...

  6. Python基础教程总结(一)

    引言: 一直都听说Python很强大,以前只是浏览了一些博客,发现有点像数学建模时使用的Matlab,就没有深入去了解了.如今Python使用的地方越来越多,最近又在学习机器学习方面的知识,因此想系统 ...

  7. zoj 1425 最大交叉匹配

    Crossed Matchings Time Limit: 2 Seconds      Memory Limit: 65536 KB There are two rows of positive i ...

  8. sharpwebmail邮件管理系统开源 下载及使用方法

    原文发布时间为:2008-11-16 -- 来源于本人的百度文章 [由搬家工具导入] 网址:http://sourceforge.net/projects/sharpwebmail/ 点击后:点击do ...

  9. 转 Centos下安装apahce的configure: error: APR not found. Please read the documentation解决办法

    转自: http://www.cnblogs.com/Anker/p/3355573.html 今天从Apache官网上http://httpd.apache.org/下载httpd web服务器,由 ...

  10. Firmware 加载原理分析【转】

    转自:http://blog.csdn.net/dxdxsmy/article/details/8669840 [-] 原理分析 实现机制 总结   前言 前段时间移植 wifi 驱动到 Androi ...