代码随想录Day50 - 动态规划 | 买卖股票

代码随想录

121. 买卖股票的最佳时机

给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。

你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。

返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0 。

力扣

解法1:双指针(TLE)

有重复的比较

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

解法2:反向遍历

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

122. 买卖股票的最佳时机 II

力扣

给你一个整数数组 prices ,其中 prices[i] 表示某支股票第 i 天的价格。

在每一天,你可以决定是否购买和/或出售股票。你在任何时候 最多 只能持有 一股 股票。你也可以先购买,然后在 同一天 出售。

返回 你能获得的 最大 利润 。

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        vector<vector<int>> dp(prices.size(), vector<int>(2, 0)); // 0: not has; 1: has
        dp[0][1] = -prices[0];
        for(int i = 1; i < prices.size(); i++){
            dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
            dp[i][1] = max(dp[i - 1][0] - prices[i], dp[i - 1][1]);
        }
        return dp[prices.size() - 1][0];

    }
};

309. 最佳买卖股票时机含冷冻期

力扣

给定一个整数数组prices,其中第  prices[i] 表示第 i 天的股票价格 。​设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):

  • 卖出股票后,你无法在第二天买入股票 (即冷冻期为 1 天)。

注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

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

714. 买卖股票的最佳时机含手续费

力扣

给定一个整数数组 prices,其中 prices[i]表示第 i 天的股票价格 ;整数 fee 代表了交易股票的手续费用。

你可以无限次地完成交易,但是你每笔交易都需要付手续费。如果你已经购买了一个股票,在卖出它之前你就不能再继续购买股票了。

返回获得利润的最大值。

注意:这里的一笔交易指买入持有并卖出股票的整个过程,每笔交易你只需要为支付一次手续费。

class Solution {
public:
    int maxProfit(vector<int>& prices, int fee) {
        vector<vector<int>> dp(prices.size() + 2, vector<int>(2, 0));
        if(prices.size() < 1) return 0;
        dp[0][1] = -prices[0] - fee;

        for(int i = 1; i < prices.size(); i++){
            dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
            dp[i][1] = max(dp[i - 1][0] - prices[i] - fee, dp[i - 1][1]);
        }
        return dp[prices.size() - 1][0];
    }
};

贪心解法(TODO)

代码随想录

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值