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[i][j] = max(local[i-1][j] + diff, globalMax[i-1][j-1] + max(0, diff))    ==>    localMax[j] = max(localMax[j] + diff, globalMax[j-1] + max(0, diff))

globalMax[i][j] = max(globalMax[i-1][j], localMax[i][j])      ==>    globalMax[j] = max(globalMax[j], localMax[j])

For here ,diff = prices[i+1] - prices[i]. max(0, diff) means only when the profit is positive, we add it to globalMax.

 1 class Solution {
 2 public:
 3     int maxProfit(int k, vector<int> &prices) {
 4         int len = prices.size(), result = 0;
 5         if (len < 2) return 0;
 6         if (len <= k) {
 7             for (int i = 0; i < len-1; i++) {
 8                 if (prices[i+1] - prices[i] > 0) {
 9                     result += prices[i+1] - prices[i];
10                 }
11             }
12         } else {
13             vector<int> lMax(k+1, 0), gMax(k+1, 0);
14             for (int i = 0; i < len-1; i++) {
15                 int diff = prices[i+1] - prices[i];
16                 for (int j = k; j >= 1; j--) {
17                     lMax[j] = max(gMax[j-1] + max(0, diff), lMax[j] + diff);
18                     gMax[j] = max(lMax[j], gMax[j]);
19                 }
20             }
21             result = gMax[k];
22         }
23         return result;
24     }
25 };
时间: 2024-12-15 01:40:01

LeetCode – Refresh – Best Time to Buy and Sell Stock iv的相关文章

【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 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 (i

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

This is simple, use greedy algorithm will solve the problem. 1 class Solution { 2 public: 3 int maxProfit(vector<int> &prices) { 4 int len = prices.size(), result = 0; 5 if (len < 2) return 0; 6 for (int i = 0; i < len-1; i++) { 7 if (pric

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

III is a kind of I. But it require 2 maximum value. So scan from begin and scan from end to record the maximum value for currrent index. Then scan the maximum array to obtain the global maximum. 1 class Solution { 2 public: 3 int maxProfit(vector<int

LeetCode – Refresh – Best Time to Buy and Sell Stock

1 class Solution { 2 public: 3 int maxProfit(vector<int> &prices) { 4 if (prices.size() < 2) return 0; 5 int len = prices.size(), result = 0, rec = prices[0]; 6 for (int i = 1; i < len; i++) { 7 result = max(result, prices[i] - rec); 8 rec

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