zoukankan      html  css  js  c++  java
  • [LeetCode 122]

    问题

    假设你有一个数组,其中的第i个元素表示一只股票在第i天的价格。

    设计一个算法找出最大的利润值。你可以进行任意多次的交易(即多次的卖出并买入一份股票)。你不能在同一时间进行多次交易(即你必须在再次买入股票之前卖出当前的股票)

     

    初始思路

    有了在买入与卖出股票的最佳时机III中的分析,这题就很容易得出答案了。像III中那样,我们使用3个变量来纪录利润:

    Profit currentProfit;
    Profit maxProfit;
    int totalProfit;

    currentProfit表示当前日期卖出的利润,maxProfit表示本次交易中的最大利润,totalProfit记录总利润。由于可以进行任意多次的交易,我们应当在发现利润与前一天利润相比变小时就进行一次交易,从而保证利润最大。即每当发现currentProfit小于maxProfit时,将maxProfit中的利润加到totalProfit中,并重置currentProfit和maxProfit。由于maxProfit重置为0,在价格持续减小的情况下并不会影响totalProfit的值。如[3,2,1]这种情况,我们进行了两次增加totalProfit并重置的操作,但最后结果仍然是正确答案0。

    一个需要注意的地方是[1,4]这种情况-利润并没有开始变小但是已经到了最后一天。为了处理这种情形,我们需要在对数组遍历完毕后再把maxProfit加到totalProfit一次。

    最后的完整代码如下,通过Small和Large的测试:

     1     class Solution
     2     {
     3     public:
     4         int maxProfit(std::vector<int> &prices)
     5         {
     6             Profit currentProfit;
     7             Profit maxProfit;
     8             int totalProfit = 0;
     9             
    10             for(int day = 0; day < prices.size(); ++day)
    11             {
    12                 if(currentProfit.buyPrice == -1)
    13                 {
    14                     currentProfit.buyPrice = prices[day];
    15                     currentProfit.buyDay = day;
    16                     continue;
    17                 }
    18                 
    19                 currentProfit.profit = prices[day] - currentProfit.buyPrice;
    20                 currentProfit.sellDay = day;
    21                 
    22                 if(currentProfit.profit > maxProfit.profit)
    23                 {
    24                     maxProfit = currentProfit;
    25                 }
    26                 else if(currentProfit.profit < maxProfit.profit)
    27                 {
    28                     totalProfit += maxProfit.profit;
    29                     
    30                     currentProfit.buyPrice = prices[day];
    31                     currentProfit.buyDay = day;
    32                     currentProfit.profit = 0;
    33                     
    34                     maxProfit.profit = 0;
    35                 }
    36             }
    37             
    38             if(maxProfit.profit != 0)
    39             {
    40                 totalProfit += maxProfit.profit;
    41             }
    42             
    43             return totalProfit;
    44         }
    45         
    46     private:
    47         struct Profit
    48         {
    49             Profit() : profit(0), buyPrice(-1), buyDay(0), sellDay(0)
    50             {
    51             }
    52             
    53             int profit;
    54             int buyPrice;
    55             int buyDay;
    56             int sellDay;
    57         };
    58     };
    maxProfit
  • 相关阅读:
    某开源ERP最新版SQL与RCE的审计过程
    QEMU固件模拟技术-stm32仿真分析及IRQ仿真实践
    QEMU固件模拟技术分析-luaqemu实现分析
    C/C++源码扫描系列- Fortify 篇
    C/C++源码扫描系列- Joern 篇
    C/C++源码扫描系列- codeql 篇
    bluetooth_stack开源蓝牙协议栈源码分析与漏洞挖掘
    DA14531芯片固件逆向系列(4)- L2CAP及ATT层收包再分析
    DA14531芯片固件逆向系列(3)- BLE收包流程分析及漏洞挖掘思路分享
    微服务架构简单搭建——Spring Cloud Eureka、Ribbon实现服务治理与服务消费
  • 原文地址:https://www.cnblogs.com/shawnhue/p/leetcode_122.html
Copyright © 2011-2022 走看看