LeetCode121/122/123 Best Time to Buy and Sell Stock股票 I/II

一:LeetCode 121Best Time to Buy and Sell Stock

题目:

Say you have an array for which theithelement is the price of a given stock on dayi.

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.

链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/

分析:此题就是选择买入卖出股票的最大收益,对于第i天卖出的最大收益即为第i天的股市价格减去[0,i-1]天内的最小股市价格,当第i天的股市价格比漆面最低股市价格还低,则更新最低股市价格。然后取最大的股市收益,为DP问题。用profit[i]表示第i天的收益,则minBuyPrice = min(minBuyPrice, prices[i]),并且profit[i] = prices[i]-minBuyPrice. 然后取profit中的最大值。

class Solution {public:int maxProfit(vector<int> &prices) {int n = prices.size();if(n == 0) return 0;int maxPro = 0;int minBuyPrice = prices[0];for(int i = 1; i < n; i++){minBuyPrice = min(minBuyPrice, prices[i]); // 用于记录当前天买进的最小值 minBuyPrice[i+1] = min(minBuyPrice[i], nums[i])if(maxPro < (prices[i]- minBuyPrice)){// 全局最大利益是否小于当天的利益maxPro = prices[i]- minBuyPrice;}}return maxPro;}};二:LeetCode 122Best Time to Buy and Sell Stock II

题目:

Say you have an array for which theithelement is the price of a given stock on dayi.

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

链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/

分析:此题是上题的变形,买入卖出的次数没有限制,但是第二次买入必须在第一次卖出的时间节点之后,,此时存在一个局部最优,即 2 4 5 3 6 8,此时8-2一定小于5-2+8-3,,因此就有取数组每次递增的收益即为局部最优,然后所有的局部最优加起来就是全局最优

class Solution {public:int maxProfit(vector<int> &prices) {int n = prices.size();if(n == 0) return 0;int minBuyPrice = prices[0];int sumResult = 0;/*for(int i = 0; i < n; i++){if(i+1 < n && prices[i] >= prices[i+1]){// 局部最优在于当prices[i] >= prices[i+1]sumResult += prices[i]-minBuyPrice; //既可用prices[i]-minBuyPrices了找到局部最优了–全部加起来就是全局最优解了minBuyPrice = prices[i+1];}else{if(i+1 == n) sumResult += prices[i]-minBuyPrice;}}*/for(int i = 1; i < n; i++){if(prices[i] < prices[i-1]){// 在i处有一个局部最优,所有的局部最优和就是全局最优sumResult += prices[i-1]- minBuyPrice;minBuyPrice = prices[i];}}sumResult += prices[n-1]- minBuyPrice;return sumResult;}};后来在discuss中看到一段更牛逼的代码:只要十行:

class Solution {public:int maxProfit(vector<int> &a) {int profit = 0;for (int i = 1; i < a.size(); ++i) {if (a[i] > a[i-1]) {profit += a[i]-a[i-1];}}return profit;}};三:LeetCode 123Best Time to Buy and Sell Stock III

题目:

Say you have an array for which theithelement is the price of a given stock on dayi.

Design an algorithm to find the maximum profit. You may complete at mosttwotransactions.

Note:You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/

分析:此题亦即变形,也就是求交易次数最多为两次。当然有两种方法,第一种暴力,对于每个i,我们求[0,i]与[i,n-1]两次收益,然后求和,,遍历i,可以取其中的最大值,需要O(N^2)的时间。第二种方法是动态规划,用两个数组,第一个数组f1[i]用来表示在[0,i]内进行买入卖出的最大收益,用f2[i]表示在[i,n-1]内进行买入卖出的最大收益。然后最大收益即为max(f1[i]+f2[i]),如何求f1[i]和f2[i],,第一题的方法已经给出了。

class Solution {public:int maxProfit(vector<int> &prices) {int n = prices.size();if(n <= 1) return 0;vector<int> f1(n); // 表示在[0,i]内进行买入卖出所能获得的最大profitvector<int> f2(n); // 表示在[i, n-1]内进行买入卖出所能获得的最大profit 结果就为max(f1[i]+f2[i])int minPrice = prices[0];for(int i = 1; i < n; i++){minPrice = min(minPrice, prices[i]);f1[i] = max(f1[i-1], prices[i]- minPrice);}int maxPrice = prices[n-1];for(int i = n-2; i >=0; i–){// 这里要从后往前遍历maxPrice = max(maxPrice, prices[i]);f2[i] = max(f2[i+1], maxPrice – prices[i]);}int maxResult = 0;for(int i = 0; i < n; i++)maxResult = max(maxResult, f1[i]+f2[i]);return maxResult;}};

懂得倾听别人的忠告。

LeetCode121/122/123 Best Time to Buy and Sell Stock股票 I/II

相关文章:

你感兴趣的文章:

标签云: