LeetCode 123 Best Time to Buy and Sell Stock III (C++, Python)

Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.

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)

Solution (C++)


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int profit = 0;
        if (prices.empty()) return 0;
        const int N = prices.size();
        vector<int> maxprices(N, 0);
        int tmpmaxprice = numeric_limits<int>::min();
        for (int i = N - 1; i >= 0; --i) {
            tmpmaxprice = max(tmpmaxprice, prices[i]);
            maxprices[i] = tmpmaxprice;
        }
        vector<int> firstprofits(N, 0);
        int tmpminprice = numeric_limits<int>::max();
        int tmpmaxprofit = 0;
        for (int i = 0; i < N; ++i) {
            tmpminprice = min(tmpminprice, prices[i]);
            tmpmaxprofit = max(tmpmaxprofit, prices[i] - tmpminprice);
            firstprofits[i] = tmpmaxprofit;
        }
        for (int i = 2; i <= N; ++i) {
            // second transaction buy on day i
            int secondprofit = (i == N)? 0: (maxprices[i] - prices[i]);
            profit = max(profit, firstprofits[i - 1] + secondprofit);
        }
        return profit;
    }
};

Solution (Python)


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution:
    # @param {integer[]} prices
    # @return {integer}
    def maxProfit(self, prices):
        N = len(prices)
        if N <= 1: return 0
        minPrices = [None] * N 
        tmpMinPrice = None
        for i in range(N):
            if tmpMinPrice is None:
                tmpMinPrice = prices[i]
            else:
                tmpMinPrice = min(tmpMinPrice, prices[i])
            minPrices[i] = tmpMinPrice
        secondProfits = [None] * N
        tmpMaxPrice = None
        tmpMaxProfit = None
        for i in range(N - 1, -1, -1):
            if tmpMaxPrice is None:
                tmpMaxPrice = prices[i]
            else:
                tmpMaxPrice = max(tmpMaxPrice, prices[i])
            if tmpMaxProfit is None:
                tmpMaxProfit = tmpMaxPrice - prices[i]
            else:
                tmpMaxProfit = max(tmpMaxProfit, tmpMaxPrice - prices[i])
            secondProfits[i] = tmpMaxProfit
        ret = 0
        # first transaction sold on day i
        for i in range(N):
            firstProfit = prices[i] - minPrices[i]
            secondProfit = secondProfits[i]
            ret = max(ret, firstProfit + secondProfit)
        return ret       

Popular posts from this blog

How Django Works (4) URL Resolution

Python Class Method and Class Only Method

How Django works (3): Testing Server and Static Files Serving