LeetCode 121、122、123、188:Best Time to Buy and Sell Stock

来源:互联网 发布:易语言数据库排序 编辑:程序博客网 时间:2024/06/05 01:00

之前已经做过该系列较简单的前两道题(121、122),今天将123、188题一齐解了,发现挑战难度提高了许多。于是决定将这四道题的解法汇总在这篇博客里,基本上用的都是动态规划或贪心算法。


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.

class Solution {public:    int maxProfit(vector<int>& prices) {        int Size = prices.size();        if (Size <= 1)             return 0;        int result = 0;        int cur_Max = prices[0];        for (int i = 1; i < Size; i++) {            if (prices[i] > cur_Max) {                result += prices[i] - cur_Max;                cur_Max = prices[i];            }        }        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 Size = prices.size();        if (Size <= 1)            return 0;        int result = 0;        for (int i = 1; i < Size; i++) {            if (prices[i] > prices[i - 1])                result += prices[i] - prices[i - 1];        }        return result;    }};

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.

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) {        int Size = prices.size();        if (Size <= 1) return 0;                int* left_profit = new int[Size];        int* right_profit = new int[Size];                int cur_min = prices[0];        left_profit[0] = 0;        for (int i = 1; i < Size; i++) {            cur_min = min(cur_min, prices[i]);            left_profit[i] = max(left_profit[i - 1], prices[i] - cur_min);        }                int cur_max = prices[Size - 1];        right_profit[Size - 1] = 0;        for (int i = Size - 2; i >= 0; i--) {            cur_max = max(cur_max, prices[i]);            right_profit[i] = max(right_profit[i + 1], cur_max - prices[i]);        }                int profit = 0;        for (int i = 0; i < Size; i++) {            profit = max(profit, left_profit[i] + right_profit[i]);        }        return profit;    }};

188. Best Time to Buy and Sell Stock IV [题目链接] 


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 k 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(int k, vector<int>& prices) {        if(prices.size()<=1)            return 0;        //vector< vector<int> > local(prices.size()+1,vector<int>(k+1));        //vector< vector<int> > global(prices.size()+1,vector<int>(k+1));        if (k>prices.size()/2){ // simple case            int ans = 0;            for (int i=1; i<prices.size(); ++i){                ans += max(prices[i] - prices[i-1],0);            }            return ans;        }        vector<int> local(k+1);        vector<int> global(k+1);        for(int i = 0; i < prices.size() - 1; i++)        {            int diff = prices[i+1] - prices[i];            for(int j = k - 1; j >= 0; j--)            {                local[j + 1] = max(local[j + 1] + diff, global[j] + max(0, diff));                global[j + 1]= max(global[j + 1],local[j + 1]);            }        }        return global[k];    }};

顺便附上动态规划和贪心算法的异同性:


阅读全文
0 0
原创粉丝点击