LeetCode 121、122、123、188:Best Time to Buy and Sell Stock
2017-12-07 19:15
477 查看
之前已经做过该系列较简单的前两道题(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.
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).
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).
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).
顺便附上动态规划和贪心算法的异同性:
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]; } };
顺便附上动态规划和贪心算法的异同性:
相关文章推荐
- leetcode_[python/C++]_121/122/123/188.Best Time to Buy and Sell Stock I/II/III/IV
- leetcode(121)(122)(123)(188) Best Time to Buy and Sell Stock I/II/III/IV JAVA代码
- 121 122 123 188 Best Time to Buy and Sell Stock
- leetcode【121+122+123 best time to buy and sell stock】【python】
- leetcode 121 122 123 . Best Time to Buy and Sell Stock
- LeetCode 121, 122, 123. Best Time to Buy and Sell Stock i, ii, iii
- LeetCode 121-123 Best Time to Buy and Sell Stock I II III
- LeetCode - 121/122 - Best Time to Buy and Sell Stock
- LeetCode 121 Best Time to Buy and Sell Stock(股票买入卖出的最佳时间)
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
- 122.leetcode Best Time to Buy and Sell Stock II(medium)[贪心算法 数组]
- 【LeetCode】123. Best Time to Buy and Sell Stock III
- LeetCode123 Best Time to Buy and Sell Stock III
- Leetcode_122_Best Time to Buy and Sell Stock II
- LeetCode122:Best Time to Buy and Sell Stock II
- Leetcde——121 Best Time to Buy and Sell Stock && 123 Best Time to Buy and Sell Stock
- Leetcode 188 Best Time to Buy and Sell Stock IV 至多k次买卖股票最大收益
- LeetCode121:Best Time to Buy and Sell Stock
- leetcode 121. Best Time to Buy and Sell Stock 最大字段和问题 + DP
- [LeetCode 121]Best Time to Buy and Sell Stock(动态规划)