题目:

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][oldboy]字符串 format

    #coding=utf8 def format(self, *args, **kwargs): # known special case of str.format """ ...

  2. 2016湖南省赛----G - Parenthesis (括号匹配)

    2016湖南省赛----G - Parenthesis (括号匹配)   Bobo has a balanced parenthesis sequence P=p 1 p 2…p n of lengt ...

  3. Mysql书签

    关于 MySQL UTF8 编码下生僻字符插入失败/假死问题的分析

  4. 【Luogu】P2536病毒检测(Trie上DP)

    题目链接 这道题我写了个01DP,f[i][j]表示跑到Trie上第i个节点,匹配到字符串第j位行不行 然后重点在*号无限匹配怎么处理 经过一番脑洞我们可以发现*号无限匹配可以拆成两种情况: 1:匹配 ...

  5. Java面试题之多线程同步和互斥有几种实现方法,都是什么?

    线程同步是指线程之间所具有的一种制约关系,一个线程的执行依赖另外一个线程的消息,当它没有得到另一个线程的消息时应等待,直到消息到达时才被唤醒. 线程互斥是指对于共享的进程系统资源,每个线程访问时的排他 ...

  6. java 时间戳与date转换

    1.时间戳转换为date long sjc=1442633777; SimpleDateFormat t = new SimpleDateFormat("yyyyMMddHHmmss&quo ...

  7. 【BZOJ3529】【SDOI2014】数表 (莫比乌斯反演+树状数组)

    传送门 Description 有一张$n\times m$的数表,其第$i$行第$j$列 $(1≤i≤n,1≤j≤m)$ 的数值为能同时整除$i$和$j$的所有自然数之和.现在给定$a$,计算数表中 ...

  8. Quotes in shell(bash, csh)

    There are three kinds of quote: single quote('), double quote("), backslash(\), how shell expla ...

  9. 标准C程序设计七---47

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  10. 42深入理解C指针之---指针与队列

    一.借助第40指针与链表的相关内容,稍微修改即可: 1.定义头文件queue.h代码如下: #include <stdlib.h> #include <stdio.h> #if ...