LeetCode || Best Time to Buy and Sell Stock

Best Time to Buy and Sell Stock

  Total Accepted: 11385  Total Submissions: 37201 My Submissions

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.


即只允许买卖一次,求最大收益值。难点在于,例如 [2, 4, 7, 1, 9],最大值为 9-2=7,但是中间会有降低的情况,7->1。论坛学习:
解法1: 动态规划

改进:减少空间复杂度至常数,因为我们不需要为每一个元素保存最大值,只需要一个即可:

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        if(prices.size()==0)
            return 0;
        int minp=prices[0], maxpro=0;
        for(int i=0; i<prices.size(); ++i){
            minp=min(minp, prices[i]);
            maxpro=max(maxpro, prices[i]-minp);
        }
        return maxpro;
    }
};
即保存当前的最小元素和最大收益,并一直更新它们即可。


Best Time to Buy and Sell Stock II

  Total Accepted: 11213  Total Submissions: 31635 My Submissions

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). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).


如果允许自由买卖,那么最大收益?
答:只需将每一个相邻两个递增元素的差值累加即可。
class Solution {
public:
    int maxProfit(vector<int> &prices) {
        int profit=0;
        for(int i=1; i<prices.size(); i++){
            if(prices[i]>prices[i-1])
                profit+=prices[i]-prices[i-1];
        }
        return profit;
    }
};

Best Time to Buy and Sell Stock III

  Total Accepted: 7068  Total Submissions: 32531 My Submissions

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, you must sell the stock before you buy again).

如果只允许两次交易,那么最大值为多少?
答:分别计算每个元素处的最大收益值,利用 动态规划,找出合适的分割点。
class Solution {
public:
    int maxProfit(vector<int> &prices) {
        if(prices.size()==0)
            return 0;
        vector<int> mp;
        vector<int> mp_reverse;
        mp.assign(prices.size(), 0);   //初始化vector长度和初始值
        mp_reverse.assign(prices.size(), 0);   //初始化vector长度和初始值

        int minp=prices[0], res=0;
        for(int i=0; i<prices.size(); ++i){
            minp=min(minp, prices[i]);
            if(i>0)
                mp[i]=max(mp[i-1], prices[i]-minp);
        }
        int maxp=prices[prices.size()-1];
        for(int i=prices.size()-1; i>=0; i--){
            maxp=max(maxp, prices[i]);
            if(i<prices.size()-1){
                mp_reverse[i]=max(mp_reverse[i+1], maxp-prices[i]);
            }
            res=max(res, mp[i]+mp_reverse[i]);
        }
        return res;
    }
};





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值