array-股票买卖问题--- Best Time to Buy and Sell Stock

121Best Time to Buy and Sell Stock

问题描述:

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.

Example 1:

Input: [7, 1, 5, 3, 6, 4]
Output: 5

max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)

Example 2:

Input: [7, 6, 4, 3, 1]
Output: 0

In this case, no transaction is done, i.e. max profit = 0.

问题解析:

1. 本题是求给定数组表示每天的股票交易价格,求在某天买入和另一天卖出的最大收益,其实就是求数组中索引在后面的减去索引再前面的数的最大差值。

2.此题的解法简单,可以在O(n)的时间复杂度。有一个记录当前最小值的min变量,然后不断更新,用当前值减去min,不断更新最大收益max,遍历完数组就能求出最大值。

代码如下:

class Solution {
public:
    // 此题利用一个min来存储之前遍历的最小值,作为买入值。然后更新收益profit和min,依次遍历更新即可
    int maxProfit(vector<int>& prices) 
    {
        if(prices.empty())
            return 0;
        int min = prices[0], profit = 0;
        int temp = 0;
        for(int i=1; i<prices.size(); ++i)
        {
            temp = prices[i] - min;
            if(temp < 0)
                min = prices[i];
            else if(temp > profit)
                profit = temp;
        }
        return profit;  
    }
};

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

问题解析:

1.此题的意思是:可以多次买入股票和卖出股票,求出最大收益,但是买入和卖出股票不会在同一天。
2. 其实本题提示你在不在同一天买入或者卖出,降低难度,因为本身也不会再同一天买入或者卖出,买入又卖出,其实相当于没有卖,还是维持之前的股票。
3. 在做此题的时候,想明白原理了,其实是只要求出,数组中每个前面比后面大的数字的差值就是最终答案。why?举个例子,数组{7,1, 5, 3, 4, 6}答案是:(5-1)+(4-3)+(6-4)=(5-1)+(6-3)= 7。其中,第四天买入,第五天卖出,第五天买入,第六天卖出,其实就是相当于第第四天买入,第六天卖出,这样求出来的必然是最大收益。对于连续递增的数,当然是在最低的时候买入,在递增的最后一个数卖出的时候,就是局部最大的收益了。把每个这样的局部最大收益加起来,就是整体的最大收益。


代码如下:

class Solution {
public:
    // 贪心法。从前向后遍历数组,只要当天的价格高于前一天的价格,就算入收益
    int maxProfit(vector<int>& prices) 
    {
        if(prices.empty())
            return 0;
        int profit = 0;
        int temp = 0;
        for(int i=1; i<prices.size(); ++i)
        {
            temp = prices[i] - prices[i-1];
            if(temp > 0)
                profit += temp;
        }
        return profit;
        
    }
};

123Best Time to Buy and Sell Stock 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, you must sell the stock before you buy again).

问题解析:

1. 这道题的意思是:最多完成两笔交易,求最大的收益。
2. 两笔交易:解决此问题可以转化为第一题。就是以当天为界限,前面的倒今天求一次最大收益,明天到最后求一次最大收益,加起来就是当天卖出第一笔交易的两笔最大收益,更新最大收益,求出最大值。
3. 为了防止每天都求两次收益,时间复杂复上升为O(n^2),设置一个辅助数组dp[n],第一次从左到右更新,表示开头到今天卖出的最大收益。完了之后再从后往前更新一遍,表示今天的下一天到数组结尾的最大收益,结果累加在第一次的dp上面。最后求出数组dp的最大值即可。

代码如下:

class Solution {
public:
    int maxProfit(vector<int>& prices) 
    {
        if(prices.empty())
            return 0;
        
        int min = prices[0], profit = 0;
        int temp = 0;
        int size = prices.size();
        // 设置辅助数组dp
        vector<int> dp(size+1, 0);
        // 从前往后更新dp
        for(int i=1; i<size; ++i)
        {
            temp = prices[i] - min;
            if(temp < 0)
            {
                min = prices[i];
            }
           if(temp > profit)
                profit = temp;
            dp[i+1] = profit;   
        }
        // 从后往前更新dp
        int max = prices[size-1]; temp=0;profit = 0;
        for(int i=size-2; i>=0; --i)
        {
            temp = max - prices[i];
            if(temp < 0)
            {
                max = prices[i];
            }
            if(temp > profit)
                profit = temp;
            dp[i] += profit;
        }
        // 寻找最大值
        profit = 0;
        for(int i=0; i<size; ++i)
            if(dp[i] > profit)
                profit = dp[i];
        return profit;
    }
};

123 Best Time to Buy and Sell Stock III
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值