LeetCode - Best Time to Buy and Sell Stock III

Best Time to Buy and Sell Stock III

2014.1.13 01:43

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

Solution:

  This problem is a variation from Best Time to Buy and Sell Stock. The difference lies in that you can make two transactions, instead of only one.

  In that problem, we pointed out a solution using dynamic programming, done in linear time complexity. Note that the DP can be done either from front to rear, or from rear to front, different code but same outcome.

  In this problem, we'll do both, i.e. do the DP from both ends and let them meet up in the middle.

  Time complexity is doubled, as you can do two transactions. Space complexity is raised from O(1) to O(n), since you have to record the intermediate results and scan for the maximum sum in the end.

Accepted code:

 1 // 1RE, 1WA, 1AC, good
 2 class Solution {
 3 public:
 4     int maxProfit(vector<int> &prices) {
 5         // IMPORTANT: Please reset any member data you declared, as
 6         // the same Solution instance will be reused for each test case.
 7         
 8         // 1RE here, didn't take empty array into account
 9         if(prices.size() <= 0){
10             return 0;
11         }
12         
13         int *a1, *a2;
14         int n;
15         int i, mv;
16         
17         n = prices.size();
18         a1 = new int[n];
19         a2 = new int[n];
20         
21         a1[0] = 0;
22         mv = prices[0];
23         for(i = 1; i < n; ++i){
24             if(prices[i] < mv){
25                 mv = prices[i];
26             }
27             // 1WA here, prices[i] not a1[i]
28             a1[i] = prices[i] - mv > a1[i - 1] ? prices[i] - mv : a1[i - 1];
29         }
30         
31         a2[n - 1] = 0;
32         mv = prices[n - 1];
33         for(i = n - 2; i >= 0; --i){
34             if(prices[i] > mv){
35                 mv = prices[i];
36             }
37             a2[i] = mv - prices[i] > a2[i + 1] ? mv - prices[i] : a2[i + 1];
38         }
39         
40         mv = a1[0];
41         for(i = 1; i < n; ++i){
42             if(a1[i] > mv){
43                 mv = a1[i];
44             }
45         }
46         
47         for(i = 0; i < n; ++i){
48             if(a2[i] > mv){
49                 mv = a2[i];
50             }
51         }
52         
53         for(i = 0; i < n - 1; ++i){
54             if(a1[i] + a2[i + 1] > mv){
55                 mv = a1[i] + a2[i + 1];
56             }
57         }
58         
59         delete[] a1;
60         delete[] a2;
61         a1 = nullptr;
62         a2 = nullptr;
63         
64         return mv;
65     }
66 };

 

转载于:https://www.cnblogs.com/zhuli19901106/p/3516813.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值