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

Поділитися
Вставка
  • Опубліковано 23 січ 2025

КОМЕНТАРІ • 16

  • @GregHogg
    @GregHogg  5 місяців тому

    Master Data Structures & Algorithms for FREE at AlgoMap.io/

  • @thomasbooth2778
    @thomasbooth2778 5 місяців тому +2

    Theres also a really nice greedy solution to this also. Where it mentions 'You can buy it then immediately sell it on the same day.' Was something I overlooked at the start and wrote a solution like yours. Definitely worth noting to look for small bits of info like this in problems

  • @paranoiak669
    @paranoiak669 2 місяці тому +2

    My solution was this code :
    profit = 0
    for i in range(len(prices)-1):
    diff = prices[i+1]-prices[i]
    if diff>0:
    profit += diff
    return profit
    No need to calculate low and high here.
    But as always, great video !

  • @JoeTan-nq4fq
    @JoeTan-nq4fq 4 місяці тому +1

    Love your videos. Concise and easy to understand.

  • @RuoyanJiang
    @RuoyanJiang 4 місяці тому

    Love your video, it was concise, the solution was easy to understand and definitely helped me prepare for the interview, thanks :)

  • @angelomarano8458
    @angelomarano8458 5 місяців тому +3

    love this videos, very helpfull!.

  • @ambersautner
    @ambersautner 5 місяців тому +3

    extremely thorough and easy to understand! thank you for making these videos, im planning on binging your channel lolol

    • @GregHogg
      @GregHogg  5 місяців тому

      Binge it up at algomap.io :)

  • @JoeTan-nq4fq
    @JoeTan-nq4fq 4 місяці тому

    It would be similar that we calculate the difference of today's and y'day's price for all prices. The max profit would be the sum of all the profits (ignore the losses).

  • @gyk9738
    @gyk9738 Місяць тому

    1:38 btw I am an intel employee

  • @pawelrolbiecki7813
    @pawelrolbiecki7813 3 місяці тому

    But you can buy and sell it the same day. So why don't we do it "scalping" way? make profit "day by day" ? It simplifies the aproach and it is fatser.

  • @testter-u5j
    @testter-u5j 3 місяці тому

    Intel lol 🤣🤣