Please explain the problem more clearly!!!


  • 80
    E

    "If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit."

    I misunderstood the above sentence as at most either buy or sell one share of stock on a single day. In that case, I buy at a local minimum price and sell it at a local maximum to reach a maximum total profit.

    Please consider changing it to something like the following:

    If you were only permitted to complete at most one buy and at most one sell during the whole period of days, design an algorithm such that your profit, i.e., sell price minus buy price, is maximized. Note that you can only not sell a stock before you buy one.

    For example:

    [1,2,3,4] ==> returns 3 (buy at 1 and sell at 4)

    [4,3,2,1] ==> returns 0 (don't buy)

    [4,10,25,2,10] ==> returns 21 (buy at 4 and sell at 25)


  • 3
    T

    Same here, I don't know anything about stocks, nor I want to learn just to understand this problem, please be more verbose.

    Another thing that might be worth doing is putting the whole problem into past tense (what was time when I could've achieved the most profit), or into magic future where you can predict stock prices.


  • 0
    N

    Good explanation for the understanding the problem!


  • 1
    M

    Two others suggestions. First, the title of the question because the output is the max potential profit and not the 'best time'. Second, they should replace the phrase 'design an algorithm' with something like 'compute the optimal profit', since in a trading context an algorithm is usually understood to be an online algorithm (and this question is an offline question).


  • 0
    A

    You are correct, the problem description is inaccurate. Your wording is freaking spot on. surprised leetcode hasnt fixed it yet.


  • 0

    Had the same confusion when I first encoutered BuyAndSell II, which I did before this one.


  • 0
    R

    Yes! I have the same confusion with you. The question is not clear!


Log in to reply
 

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