LeetCode之“动态规划”:Best Time to Buy and Sell Stock I && II && III && IV

Best Time to Buy and Sell Stock I

  题目链接

  题目要求:

  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.

  这道题的本质在于找出一个数组中任意两个数(序号大的数减去序号小的数)的最大差值。我们发现数组某一段的最大差值信息是可以保存的,并作为下一段的初始数值。具体的递推式如下:

dp[i] = max(dp[i - 1], prices[i] - minPrice);

  程序也是比较简单:

 1 class Solution {
 2 public:
 3     int maxProfit(vector<int>& prices) {
 4         int sz = prices.size();
 5         if(sz == 0)
 6             return 0;
 7             
 8         vector<int> dp(sz, 0);
 9         int minPrice = prices[0];
10         for(int i = 1; i < sz; i++)
11         {
12             dp[i] = max(dp[i - 1], prices[i] - minPrice);
13             if(minPrice > prices[i])
14                 minPrice = prices[i];
15         }
16         
17         return dp[sz - 1];
18     }
19 };
View Code

  另一个很有代表性的解法如下(参考自一博文):

  按照股价差价构成一个新的数组,即prices[1]-prices[0], prices[2]-prices[1], prices[3]-prices[2], ..., prices[n-1]-prices[n-2],这样我们的问题就转变成求最大的连续子段和。具体如何高效求解最大连续子段和请参考自我之前写过的一篇博文

Best Time to Buy and Sell Stock II

  LeetCode没有提供题目,源自其他博文

  题目要求:

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

   这道题的解法更加简单,我们这需要先构建一个股价差数组,然后把该数组中所有大于0的数相加就能够得到结果。具体程序引自同一篇博文

 1 class Solution {
 2 public:
 3     int maxProfit(vector<int> &prices) {
 4         // IMPORTANT: Please reset any member data you declared, as
 5         // the same Solution instance will be reused for each test case.
 6         int len = prices.size();
 7         if(len <= 1)return 0;
 8         int res = 0;
 9         for(int i = 0; i < len-1; i++)
10             if(prices[i+1]-prices[i] > 0)
11                 res += prices[i+1] - prices[i];
12         return res;
13     }
14 };
View Code  

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

  因为只能完成最多两次交易,而且第二次交易只能在第一次交易完成后进行,因此我们可以先按第一题方法,按顺序求得最大和数组,再逆序求得最大和数组,最后再跟据这两个数组求得最大收益。具体程序如下:

 1 class Solution {
 2 public:    
 3     int simpleMaxProfit(vector<int>& prices) {
 4         int sz = prices.size();
 5         if(sz == 0)
 6             return 0;
 7         
 8         vector<int> dp(sz, 0);
 9         int minPrice = prices[0];
10         for(int i = 1; i < sz; i++)
11         {
12             dp[i] = max(dp[i - 1], prices[i] - minPrice);
13             if(minPrice > prices[i])
14                 minPrice = prices[i];
15         }
16         
17         vector<int> dp_rev(sz, 0);
18         int maxPrice = prices[sz - 1];
19         for(int i = sz - 2; i > -1; i--)
20         {
21             dp_rev[i] = min(dp_rev[i + 1], prices[i] - maxPrice);
22             if(maxPrice < prices[i])
23                 maxPrice = prices[i];
24         }
25         
26         vector<int> maxGain(sz, 0);
27         maxGain[0] = 0;
28         for(int i = 1; i < sz; i++)
29         {
30             maxGain[i] = max(maxGain[i - 1], dp[i] - dp_rev[i]);
31         }
32         
33         return maxGain[sz - 1];
34     }
35 
36     int maxProfit(vector<int>& prices) {
37         return simpleMaxProfit(prices);
38     }
39 };
View Code

 

转载于:https://www.cnblogs.com/xiehongfeng100/p/4569594.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值