LeetCode 122, 123, 188. Best Time to Buy and Sell Stock II+III+IV

Best Time to Buy and Sell Stock II

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int res=0;
        for (int i=1;i<prices.size();++i){
            if (prices[i]>prices[i-1]){
                res += prices[i]-prices[i-1];
            }
        }
        return res;
    }
};

Best Time to Buy and Sell Stock III

比较难想到,分别记录四个状态: 1buy,1buy1sell,2buy1sell,2buy2sell。

构建四个dp数组,例如,1buy1sell[i] = max( 1buy1sell[i-1], 1buy[i-1]+prices[i] )

由于dp只依赖于上一个状态,可以压缩空间。

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int n=prices.size();
        int dp[2][4]; // 0 - 1 buy, 1 - 1 buy 1 sell, 2 - 2 buy 1 sell, 3 - 2 buy 2 sell
        for (int i=0;i<4;++i)
            dp[0][i] = INT_MIN/2;

        for (int i=0;i<n;++i){
            dp[1][0] = max(dp[0][0],-prices[i]);
            dp[1][1] = max(dp[0][1],dp[0][0]+prices[i]);
            dp[1][2] = max(dp[0][2],dp[0][1]-prices[i]);
            dp[1][3] = max(dp[0][3],dp[0][2]+prices[i]);
            swap(dp[0],dp[1]);
        }
        return max(0, max(dp[0][1],dp[0][3]));
    }
};

Best Time to Buy and Sell Stock IV

上一题的拓展,思路一样。corner case 比较多,需要特别注意。当 k > n/2 的时候,说明可以不限次数,本题就变成了 Best Time to Buy and Sell Stock II,使用 II 的方法,可以大大减少dp所需的时间和空间。

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

        if (n<2) return 0;
        if (k>n/2){ // Best Time to Buy and Sell Stock II
            int ans = 0;
            for (int i=1; i<n; ++i)
                ans += max(prices[i] - prices[i-1],0);
            return ans;
        }

        // 0 - 1 buy, 1 - 1 buy 1 sell, 2 - 2 buy 1 sell, 3 - 2 buy 2 sell ...
        vector<vector<int>> dp(2,vector<int>(2*k));
        for (int j=0;j<2*k;++j)
            dp[0][j] = INT_MIN/2;

        for (int i=0;i<n;++i){
            dp[1][0] = max(dp[0][0],-prices[i]);
            for (int j=1;j<2*k;++j){
                dp[1][j] = max( dp[0][j], dp[0][j-1]+(j%2==1?1:-1)*prices[i] );
            }
            swap(dp[0],dp[1]);
        }
        int res=0;
        for (int j=1;j<2*k;j+=2)
            res = max(res,dp[0][j]);
        return res;
    }
};

原文地址:https://www.cnblogs.com/hankunyan/p/11258505.html

时间: 2024-08-27 15:57:01

LeetCode 122, 123, 188. Best Time to Buy and Sell Stock II+III+IV的相关文章

121. 122. 123. 188. Best Time to Buy and Sell Stock *HARD* -- 买卖股票

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.

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练题——122. Best Time to Buy and Sell Stock II

1.题目 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票). 示例 1: 输入: [7,1,5,3,6,4]输出: 7解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 .  随后,在第 4 天(股票价格 = 3)的时候买入,在第 5

【leetcode】122.Best Time to Buy and Sell Stock II

@requires_authorization @author johnsondu @create_time 2015.7.19 21:01 @url [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) /************************ * @description: dynamic programming. * 相邻元素做

[LeetCode][JavaScript]Best Time to Buy and Sell Stock II

Best Time to Buy and Sell Stock II 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 sha

【LeetCode】Best Time to Buy and Sell Stock II

Best Time to Buy and Sell Stock II 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 sha

【LeetCode】【Python题解】Best Time to Buy and Sell Stock II

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). Ho

Best Time to Buy and Sell Stock II leetcode java

题目: 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 II

sched.c sched.h 代码分析笔记 首先上header file sched.h #ifndef _SCHED_H #define _SCHED_H #define HZ 100 #define NR_TASKS 64 #define TASK_SIZE 0x04000000 #define LIBRARY_SIZE 0x00400000 #if (TASK_SIZE & 0x3fffff) #error "TASK_SIZE must be multiple of 4M&qu