[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 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>
时间: 2024-10-11 22:18:18

[LeetCode][Java] Best Time to Buy and Sell Stock IV的相关文章

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

【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 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 t

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 algorithm to find the maximum profit. You may complete at most k transactions. 解题思路: 本题是Best Time to Buy and Sell Stock系列最难的一道,需要用到dp,JAVA实现如下: public i

[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 given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm

Leetcode #188 Best Time to Buy and Sell Stock IV

题目链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ 当 k ≥ prices.size() / 2 时:题目等价于 k 无限大的情形. 当 k < prices.size() / 2 时: 用dp[m][n+1]表示在[0,n]区间,进行了m次买卖操作,获得的最大利润. 那么这个利润必为以下几个数据的最大值: dp[m-1][n+1],即在[0,n]区间,进行了m-1次买卖操作,获得的最大利润. dp[m]

LeetCode – Refresh – Best Time to Buy and Sell Stock iv

This is combination of II and III. 1. when k >= length, it is totally II. 2. when k < length, it is another III. A localMaximum and globalMaximum subarray need to be maintained. But we can reduce the two dimensional DP to one dimensional DP localMax

LeetCode 188. Best Time to Buy and Sell Stock IV (动态规划)

题目 题意:给你一个数组代表每天的股价.你有k次买入和卖出的机会,问你最多能赚多少钱.买入之前必须卖出已有股份.同一天是可以先买,再卖,或者先卖再买的. 题解:题目没有说数据范围,但是经过我实际测试 k 最大为10^8 ,n最大为10^4.当然k最多只需要取n/2就好了,因为当天买当天卖是没有意义的. 那这道题的效率就应该控制在O(n*k),再加一点就要超时了,所以两个循环嵌套.第一层是k,第二层是n 状态数组dp[k][i] 表示开始第k次买卖的交易时,第i天口袋里可以赚到的最多的钱.那么状态

[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 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,

[LeetCode OJ] Best Time to Buy and Sell Stock I

Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. eg:输