[LeetCode]Best Time to Buy and Sell Stock 1 2 3

1.

 

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.

复杂度:O(N)

 

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

 

 

 

 

2.

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

注意: you must sell the stock before you buy again. 就是说只能有一个stock在手中。否则的话,问题会很复杂。

之前没有注意这个条件。想错了。

方法:寻找连续递增区间。

复杂度:O(N)

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

 

 

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        int n = prices.size();
        if (n == 0) return 0;
        int res = 0;
        auto &a = prices;
        int s = 0, e;
        while (s < n) {
            e = s + 1;
            while (e < n && a[e] >= a[e-1]) e++;
            res += a[e-1] - a[s];
            s = e;
        }
        return res;
    }
};

 

3.

 

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

注意:第三问还是需要注意    you must sell the stock before you buy again
因为只能持有1stock。所以两次transactions一定是相互分开的。例如:1 2 5 3 6。答案应该是7 = (1,5) (3,6);而不是8 = (1,5) (2,6);
方法:把prices[1~n]分成prices[1~i]和prices[i+1~n]两段,然后在两段内各做一次transaction。如果能用O(1)的方式在prices[1~i] and prices[i+1~n]内找到最大profit。就ok了。
参考问题1中的方法。可以O(N)扫一遍: 记录下在第i天之前做一次交易的最大值。则可以解决在prices[1~i]的找最大值的问题。而i+1~n其实就是把从n开始扫一边数组,记录下 i+1~n之间的最大值。

 

复杂度:O(N)

 

#define min(a,b) ((a) > (b) ? (b) : (a))
#define max(a,b) ((a) < (b) ? (b) : (a))

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        if (prices.size() < 2) return 0;
        int s = prices.size();
        int *a = new int[s+1];
        int *b = new int[s+1];
        memset(a, 0, 4*s+4);
        memset(b, 0, 4*s+4);
        
        int curm = prices[0];
        a[0] = 0;
        for (int i = 1; i < s; ++i) {
            a[i] = max(a[i-1], prices[i] - curm);
            curm = min(curm, prices[i]);
        }
        
        curm = prices[s-1];
        b[s-1] = 0;
        for (int i = s-2; i >=0; --i) {
            b[i] = max(b[i+1], curm - prices[i]);
            curm = max(curm, prices[i]);
        }
        
        int res = 0;
        for (int i = 0; i < s; i++) {
            res = max(res, a[i] + b[i+1]);
        }
        
        delete []a;
        delete []b;
        return res;
    }
};

 

 

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        int n = prices.size();
        if (n == 0) return 0;
        vector<int> r1(n,0);
        vector<int> r2(n,0);
        auto &a = prices;
        
        int m = a[0];
        for (int i = 1; i < n; i++) {
            r1[i] = max(r1[i-1], a[i] - m);
            m = min(a[i], m);
        }
        m = a[n-1];
        for (int i = n - 2; i >= 0; i--) {
            r2[i] = max(r2[i+1], m - a[i]);
            m = max(a[i], m);
        }
        int res = r2[0];
        for (int i = 0; i < n - 1; i++) {
            res = max(res, r1[i] + r2[i+1]);
        }
        res = max(res, r1[n-1]);
        
        return res;
    }
};

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值