zoukankan      html  css  js  c++  java
  • LeetCode

    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 };
  • 相关阅读:
    npm 常用命令
    vue router 配合transition 切换动画
    非指针 复制对象和数组的两种方法
    Date()对象的设置与解析
    js map()处理数组和对象数据
    鉴别JS数据类型的全套方法
    JS数组与对象的遍历方法大全
    js异步原理与 Promise
    HTTP请求封装:Ajax与RESTful API
    "unresolved reference 'appium' "问题解决
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3516813.html
Copyright © 2011-2022 走看看