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 };
  • 相关阅读:
    shell脚本sed的基本用法
    shell grep的基本用法
    禁止表单提示输入--autocomplete属性
    Cookie操作介绍
    JSP中的两种重定向
    SSM
    题解 P4994 【终于结束的起点】
    题解 P1286 【两数之和】
    题解 P2340 【奶牛会展】
    题解 CF450B 【Jzzhu and Sequences】
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3516813.html
Copyright © 2011-2022 走看看