[LeetCode] 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).

解题思路

采用动态规划来解决问题。

我们需要维护如下两个量:

global[i][j]:当前到达第i天最多可以进行j次交易,所得到的最大利润。

local[i][j]:当前到达第i天最多可以进行j次交易,而且最后一次交易在当天卖出,所得到的最大利润。

状态转移方程:

global[i][j] = max(local[i][j], global[i-1][j])

上述方程比较两个量的大小:①当前局部最大值;②过往全局最大值。

local[i][j] = max(global[i-1][j-1] + max(diff, 0), local[i-1][j] + diff)

上述方程比较两个量的大小:

①全局到i-1天进行j-1次交易,然后加上今天的交易(如果今天的交易赚钱的话)。

②取局部第i-1天进行j次交易,然后加上今天的差值(local[i-1][j]是第i-1天卖出的交易,它加上diff后变成第i天卖出,并不会增加交易次数。无论diff是正还是负都要加上,否则就不满足local[i][j]必须在最后一天卖出的条件了)

另外需要注意的一个问题是,当k远大于数组的大小时,上述算法将变得低效。因此将其改用不限交易次数的方式解决。

实现代码

/*****************************************************************
    *  @Author   : 楚兴
    *  @Date     : 2015/2/22 23:42
    *  @Status   : Accepted
    *  @Runtime  : 15 ms
******************************************************************/
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
    int maxProfit(int k, vector<int> &prices) {
        int len = prices.size();
        if (len == 0)
        {
            return 0;
        }
        if (k >= len)
        {
            return helper(prices);
        }

        vector<int> max_local(k + 1, 0);
        vector<int> max_global(k + 1, 0);
        int diff;
        for (int i = 0; i < len - 1; i++)
        {
            diff = prices[i + 1] - prices[i];
            for (int j = k; j >= 1; j--)
            {
                max_local[j] = max(max_global[j - 1] + max(diff, 0), max_local[j] + diff);
                max_global[j] = max(max_local[j], max_global[j]);
            }
        }

        return max_global[k];
    }

    int helper(vector<int> &prices)
    {
        int profit = 0;
        for (int i = 0; i < prices.size() - 1; i++)
        {
            profit = max(profit, profit + prices[i + 1] - prices[i]);
        }

        return profit;
    }
};
时间: 2024-09-29 21:23:23

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

[LeetCode] 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, yo

LeetCode——Best Time to Buy and Sell Stock IV

Description: 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

leetcode—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. You may not engage in multiple transactions at the same time (ie, you

【dp】leetcode Best Time to Buy and Sell Stock IV

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/ [题意] 给定n天股市的票价,最多交易k次,问最大盈利是多少,手上不能同时持有两支股票. [思路] 考虑加入当天买当天卖这种情况,问题转换为n天总共交易k次(不是最多),因为后一次的买一定在前一次卖之后,所以需要dp[i][j]表示前i天交易j次且最后一次交易在第i天的最大利润.则 dp[i][j]=max(a[i]-a[k]+global[

leetcode——Best Time to Buy and Sell Stock III 买卖股票最大收益(AC)

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: Best Time to Buy and Sell Stock II [122]

[题目] 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 as many transactions as you like (ie, buy one and sell one share of the stock multiple times

LeetCode: Best Time to Buy and Sell Stock III [123]

[题目] 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 I &amp;&amp; II

一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换.要求只买卖一次,获得最大收益. 思路:一开始我认为是寻找最大.最小值,但由于最大值不一定总是出现在最小值的后面,因此WA. 参考思路:DP.对第i个价格,减去前i-1个价格中的最小值(保证该收益是在第i个价格卖出的最大收益),其收益与之前获得的最大收益相比. 代码: 1 public int max

LeetCode: Best Time to Buy and Sell Stock [121]

[题目] 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.