Best Time to Buy and Sell Stock - Leetcode 121 - Arrays & Strings (Python)

Поделиться
HTML-код
  • Опубликовано: 1 окт 2024

Комментарии • 19

  • @GregHogg
    @GregHogg  2 месяца назад +1

    Master Data Structures & Algorithms For FREE at AlgoMap.io!

  • @ngneerin
    @ngneerin 3 месяца назад +6

    min_price = float('inf')
    max_profit = 0
    for price in prices:
    min_price = min(min_price, price)
    max_profit = max(max_profit, price - min_price)
    return max_profit

  • @microscorpi0n
    @microscorpi0n Месяц назад +3

    The hardest part about my approaching this problem is the fact that LC's topic tag is DP. I spent 2+ hours trying to identify the recurrence relationship when this solution was more straightforward. Appreciate that your algomap roadmap categorizes this as Arrays and Strings. I suppose that may have helped with my approach. I didn't realize this problem was in your roadmap until after I attempted it after working on LC's daily challenge from Aug 17, 2024 was LC1937 - Maximum Number of Points with Cost, which suggested the Buy and Sell Stock 1 problem as helpful for recognizing the approach to solving.

    • @mhmdshaaban
      @mhmdshaaban 16 дней назад

      It was misleading for me as well. I usually spend some time trying to come up with different solutions, even if I find an efficient one. After I checked the tags and saw DP mentioned, I was like, what the hell is DP doing here?

    • @fernandolucas2740
      @fernandolucas2740 14 дней назад

      Totally Agree. Same situation Here.

  • @new-anointingaremu3597
    @new-anointingaremu3597 3 месяца назад +4

    Why do you always use infinity instead of not initializing the variable at all or setting the variable to 0 instead of infinity

    • @jamestacular
      @jamestacular 3 месяца назад +2

      If min_price was defaulted to 0 then none of the prices would ever fall underneath it. The point is to set a variable that for certain will be overwritten by the first value (prices[0])

    • @yairkaz
      @yairkaz 3 месяца назад +2

      @@jamestacular You can instead initialize min_price to the first variable in the array and in the O(n^2) solution you could have just set max_profit to 0.

    • @sierraobi311
      @sierraobi311 Месяц назад

      @@jamestacularhow is this true? Say you have an array of practices where all the numbers are non-zero. Why wouldn’t min price be overwritten? Just depends on how you set up the if clause I suppose.

    • @Infinitely16
      @Infinitely16 Месяц назад

      @@sierraobi311 the problem constraints say "0

  • @keelanurnishanth5801
    @keelanurnishanth5801 16 дней назад

    This was my attempt without keeping any complexities in mind. It fails at a later test case when prices =[2,4,1] and expected is 2 and my output is 0
    min_val=min(prices)
    min_index=prices.index(min_val)
    max_val=max(prices[min_index:])
    max_index=prices.index(max_val)
    if min_val==max_val :
    return 0
    else:
    return max_val-min_val

  • @_sharkdev_
    @_sharkdev_ 3 месяца назад

    In the brute force solution, is the check for if profit > 0 necessary? I don't work with python, so does its max function have issues when handling zero as an argument?

  • @sgetti4dinner
    @sgetti4dinner 3 месяца назад

    Why do we want to keep track of max profit and not max num?

    • @GregHogg
      @GregHogg  3 месяца назад +1

      Because that's what we're trying to find

  • @tuandino6990
    @tuandino6990 2 месяца назад +1

    I solved this using dp

    • @joaoc4508
      @joaoc4508 Месяц назад

      could you explain how to use dp here?

    • @hamadrehman853
      @hamadrehman853 Месяц назад

      @@joaoc4508 check neetcode videos. he uses a two pointer approach for this. in neetcode blind 75 series this is the 2nd problem he solves. I came here because his approach was too confusing.