Why [3,2,6,5,0,3] expects 7?


  • 0
    P

    I'm getting this "Wrong Answer" error. Why is 7 expected?

    Input:    [3,2,6,5,0,3]
    Output:	  9
    Expected: 7
    
    3 - buy
    2 - buy
    6 - sell 
    5 - sell
    0 - buy
    3 - sell
    

    I believe that profit should be (6+5+3) - (3+2+0) = 9


  • 1
    S

    Pay attention to it

    However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

    So, it is impossible to buy 3 then buy 2 without sell 3.

    For this test case [3,2,6,5,0,3], best way is buy on 2, sell on 6, then buy on 0 and sell on 3. Total benefit is 7.


  • 0
    P

    Ohh, ok, makes sense, my bad. I complicated it too much :) Thanks for your prompt response.


  • 0
    R

    buy on 2, sell on 6


  • 0
    S

    @renzherl, thanks! I have corrected my typo.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.