LeetCode DAY43(123. Best Time to Buy and Sell Stock III&188. Best Time to Buy and Sell Stock IV)

Preface

This is a new day to continue my Dynamic Programming journey.
Learn something new and keep reviewing what I learnt before.

1. Best Time to Buy and Sell Stock III

LeetCode Link: 123. Best Time to Buy and Sell Stock III
You are given an array prices where prices[i] is the price of a given stock on the ith day.

Find the maximum profit you can achieve. You may complete at most two transactions.

Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).

Example 1:

Input: prices = [3,3,5,0,0,3,1,4]
Output: 6
Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3.
Example 2:

Input: prices = [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are engaging multiple transactions at the same time. You must sell before buying again.
Example 3:

Input: prices = [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

Constraints:

1 <= prices.length <= 10^5
0 <= prices[i] <= 10^5

Analysis and Solution

Dynamic Programming

LeetCode C++ as followings Dynamic Programming

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        if (prices.size() == 0) return 0;//remove case 0
        vector<vector<int>> dp(prices.size(), vector<int>(5, 0));
        dp[0][1] = -prices[0];//initialization buy firstly
        dp[0][3] = -prices[0];//second buy 
        for (int i = 1; i < prices.size(); i++) {//traverse from front to end
            dp[i][0] = dp[i - 1][0];//nothing with transaction
            dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] - prices[i]);//buy firstly
            dp[i][2] = max(dp[i - 1][2], dp[i - 1][1] + prices[i]);//sold firstly or hold
            dp[i][3] = max(dp[i - 1][3], dp[i - 1][2] - prices[i]);//buy secondly 
            dp[i][4] = max(dp[i - 1][4], dp[i - 1][3] + prices[i]);//sold secondly
        }
        return dp[prices.size() - 1][4];
    }
};

2. Best Time to Buy and Sell Stock IV

LeetCode Link: 188. Best Time to Buy and Sell Stock IV
You are given an integer array prices where prices[i] is the price of a given stock on the ith day, and an integer k.

Find the maximum profit you can achieve. You may complete at most k transactions.

Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).

Example 1:

Input: k = 2, prices = [2,4,1]
Output: 2
Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.
Example 2:

Input: k = 2, prices = [3,2,6,5,0,3]
Output: 7
Explanation: Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4. Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.

Constraints:

1 <= k <= 100
1 <= prices.length <= 1000
0 <= prices[i] <= 1000

Analysis and Solution

Dynamic Programming

LeetCode C++ as followings Dynamic Programming

class Solution {
public:
    int maxProfit(int k, vector<int>& prices) {

        if (prices.size() == 0) return 0;//remove case 0
        vector<vector<int>> dp(prices.size(), vector<int>(2 * k + 1, 0));//two demensional array
        for (int j = 1; j < 2 * k; j += 2) {//buy or hold at j is odd number
            dp[0][j] = -prices[0];
        }
        for (int i = 1;i < prices.size(); i++) {
            for (int j = 0; j < 2 * k - 1; j += 2) {//sold or hold at j is even number
                dp[i][j + 1] = max(dp[i - 1][j + 1], dp[i - 1][j] - prices[i]);//get the maxProfit to buy or hold
                dp[i][j + 2] = max(dp[i - 1][j + 2], dp[i - 1][j + 1] + prices[i]);//get the maxProfit to sold or hold
            }
        }
        return dp[prices.size() - 1][2 * k];
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值