121. Best Time to Buy and Sell Stock I II III IV

来源:互联网 发布:黑蜘蛛软件 编辑:程序博客网 时间:2024/04/29 20:31

121. Best 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: 5max. 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: 0In this case, no transaction is done, i.e. max profit = 0.
class Solution {public:    int maxProfit(vector<int>& prices) {        if(prices.empty()) return 0;        int result = 0;        for(int i = 0; i< prices.size();){            int j = i+1;            while(j < prices.size() && prices[j]>=prices[i] ){                result = max(result,prices[j] - prices[i]);                j++;            }            i = j;        }        return result;    }};//解法二:连续最大数组子段和class Solution {public:    int maxProfit(vector<int>& prices) {        if(prices.empty()) return 0;        int result = 0;        int curMax = 0;        for(int i = 1; i< prices.size();i++){            curMax += prices[i] - prices[i-1];            curMax = max(0,curMax);            result = max(result,curMax);        }        return result;    }};

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

//贪心算法class Solution {public:    int maxProfit(vector<int>& prices) {        int maxCur = 0;        for(int i= 1;i < prices.size();i++){            int temp = prices[i]-prices[i-1];            if(temp > 0) maxCur += temp;        }        return maxCur;    }};

123. Best 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.

class Solution {public:    int maxProfit(vector<int>& prices) {        // f[k, ii] represents the max profit up until prices[ii] (Note: NOT ending with prices[ii]) using at most k transactions.         // f[k, ii] = max(f[k, ii-1], prices[ii] - prices[jj] + f[k-1, jj]) { jj in range of [0, ii-1] }        //          = max(f[k, ii-1], prices[ii] + max(f[k-1, jj] - prices[jj]))        // f[0, ii] = 0; 0 times transation makes 0 profit        // f[k, 0] = 0; if there is only one price data point you can't make any money no matter how many times you can trade        int KK = 2;  //特例        int res = 0;        if(prices.size() < 1) return res;        vector<vector<int> > f(KK+1,vector<int>(prices.size(),0));        for(int k = 1; k <= KK; k++){            int tmpMax = f[k-1][0] - prices[0];            for(int ii = 1; ii < prices.size(); ii++){                f[k][ii] = max(f[k][ii-1], tmpMax + prices[ii]);                tmpMax = max(tmpMax,f[k-1][ii] - prices[ii]);                res = max(res,f[k][ii]);            }        }        return res;    }};

188. Best Time to Buy and Sell Stock IV

class Solution {public:    int maxProfit(int KK, vector<int>& prices) {        int res = 0;        if(prices.size() < 1) return res;        if (KK>prices.size()/2){ // simple case            for (int i=1; i<prices.size(); ++i){                res += max(prices[i] - prices[i-1],0);            }            return res;        }        // f[k, ii] represents the max profit up until prices[ii] (Note: NOT ending with prices[ii]) using at most k transactions.         // f[k, ii] = max(f[k, ii-1], prices[ii] - prices[jj] + f[k-1, jj]) { jj in range of [0, ii-1] }        //          = max(f[k, ii-1], prices[ii] + max(f[k-1, jj] - prices[jj]))        // f[0, ii] = 0; 0 times transation makes 0 profit        // f[k, 0] = 0; if there is only one price data point you can't make any money no matter how many times you can trade        //int KK = 2;             vector<vector<int> > f(KK+1,vector<int>(prices.size(),0));        for(int k = 1; k <= KK; k++){            int tmpMax = f[k-1][0] - prices[0];            for(int ii = 1; ii < prices.size(); ii++){                f[k][ii] = max(f[k][ii-1], tmpMax + prices[ii]);                tmpMax = max(tmpMax,f[k-1][ii] - prices[ii]);                res = max(res,f[k][ii]);            }        }        return res;    }};
0 0
原创粉丝点击