随笔- 509  文章- 0  评论- 151  阅读- 22万 

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 };
复制代码

 

 posted on   zhuli19901106  阅读(256)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示