题目:

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. Ruby 符号【转】

    Ruby的符号足以让很多初学者迷惑上一段时间,看过本章节后,或许会解开你心中的疑惑. 在Ruby中,一个符号是就是一个Symbol类的实例,它的语法是在通常的变量名前加一个冒号,如 :my_sy Ru ...

  2. iOS学习笔记33-UICollectionView入门

    一.UICollectionView介绍 UICollectionView和UICollectionViewController类是iOS6新引进的API,用于展示集合视图,布局更加灵活,可实现多列布 ...

  3. Java面试题之什么情况下会触发类的初始化

    以下情况会触发类的初始化: 遇到new,getstatic,putstatic,invokestatic这4条指令: 使用java.lang.reflect包的方法对类进行反射调用: 初始化一个类的时 ...

  4. visual studio NuGet 常用包管理命令

    visual studio NuGet 常用包管理命令 查找包 Find-Package [包名] Find-Package [包名] -AllVersions 安装包 Install-Package ...

  5. bzoj 2330 [SCOI2011]糖果 差分约束模板

    题目大意 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果.但是小朋友们也有嫉妒心,总是会提出一些要求,比如小明不希望小红分到的糖果比他的多,于是在分配 ...

  6. HibernateException: No Hibernate Session bound to thread

    解决No Hibernate Session bound to thread 背景交代 在使用this.getHibernateTemplate().getSessionFactory().getCu ...

  7. Codeforces 842C Ilya And The Tree 树上gcd

    题目链接 题意 给定一棵根为\(1\)的树.定义每个点的美丽值为根节点到它的路径上所有点的\(gcd\)值.但是对于每个点,在计算它的美丽值时,可以将这条路径上某个点的值变为\(0\)来最大化它的美丽 ...

  8. 表格 td中,取checkbox后几位值

    function addToPanDianDetail() { var detail_id = ""; var detail_code = ""; $(&quo ...

  9. AC日记——Housewife Wind poj 2763

    Language: Default Housewife Wind Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 10525 ...

  10. html-屏蔽按键盘空格键是滚动条向下滚动

    document.onkeydown = function(ev){ var e = ev || event; if(e.keyCode == 32){ return false; } }