Longest Happy String - Leetcode 1405 - Python

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

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

  • @WesEd17
    @WesEd17 2 года назад +12

    Thank you! I am watching at least 5 videos from you a day they’re awesome!

  • @samarthjain5015
    @samarthjain5015 Месяц назад +1

    3:03 I was over complicating this problem the whole day. Thank you Neetcode.

  • @yunaf4609
    @yunaf4609 2 года назад +16

    I don't know how you do it man
    Every single video of yours is explained so clearly and makes it so easy to understand.
    Thanks for making these :)

  • @nakasan617
    @nakasan617 Месяц назад +2

    never knew how to use heapq in the first place. Thank you

  • @ayoubalem865
    @ayoubalem865 2 года назад +11

    it's better to use array instead of a string at the end uou join the res array with an empty string, you know each time you append a new letter to the res string you are actually creating a new one that has a negative impact on your time complexity.

    • @tsunghan_yu
      @tsunghan_yu 2 года назад +1

      Yeah better to use a list as a stringbuilder.

    • @NeetCode
      @NeetCode  2 года назад +5

      Good point, i've used that pattern in previous problems but it slipped my mind.

    • @CosmicRays123
      @CosmicRays123 2 года назад +1

      @Ayoub @neetcode Does it impact on space complexity or time complexity?

    • @ayoubalem865
      @ayoubalem865 2 года назад +3

      @@CosmicRays123 On space No because we will end up with the same result, but in term of time yes because strings are immutable so each time you are trying to add a new letter to the string the compiler create another string so he should iterate over the whole string and add to it the new letter, but with the array approach he just append the new letter to the end of the array !
      I hope I could explain it well to you !

    • @Jia-Tan
      @Jia-Tan Год назад +1

      interestingly, I tried updating my code to make the result a list and it didn't affect the runtime much. Not sure how accurate leetcode is in this regard. Memory usage however was much higher. I wonder if it's something to do with how python implements strings

  • @harishsn4866
    @harishsn4866 2 года назад

    I came up with this solution except for the edge case where one of the inputs is zero. Thank you so much for your videos. You're the reason for most of the coding skills I have acquired.

  • @CreeperFace75
    @CreeperFace75 2 года назад +5

    This is the exact prob I wanted you to do THANK YOU

  • @nehascorpion
    @nehascorpion 2 года назад +2

    You are simply the BEST! Thanks a ton!

  • @vzytv
    @vzytv 2 года назад +5

    Great video! Would you consider covering #68: Text Justification?

  • @YT.Nikolay
    @YT.Nikolay 2 года назад

    I am super excited I literally solved this problem myself but the code is identical! all the same except I have 3 if statements at the beginning instead of putting 3 input arguments into a list and then loop over the list. I feel super motivated, @NeetCode thank you very much! You can't believe how the community is thankful!

  • @megadeth205
    @megadeth205 Месяц назад +2

    hello from october challenge

  • @longchikanouo4905
    @longchikanouo4905 2 года назад

    Just wondering, for a variable input scenario where I needed to use a maxHeap and I am using Javascript as my coding language. Not sure if telling the interviewer that I intend to use a heap suffice considering I will not have enough time to implement a heap from scratch.

  • @gianniprocida3332
    @gianniprocida3332 2 года назад

    Thank you so much! Amazing content!

  • @sauravdeb8236
    @sauravdeb8236 2 года назад

    Please explain this problem : 1799. Maximize Score After N Operations in your next video.

  • @RajPatel-is8em
    @RajPatel-is8em Год назад

    Appreciated bro✅✅✅✅

  • @YouGotVexed
    @YouGotVexed 2 года назад +3

    I swear you will receive such a generous donation once its all said and done

  • @abhinavpandey3356
    @abhinavpandey3356 2 года назад

    Can u explain for this a=0 b=8 c=11?I tried in java !

  • @yashpathak9285
    @yashpathak9285 2 года назад

    Hi, Can you please solve "Find Closest Palindrome" I am struggling with that question for a long time.

  • @abhi23pawar
    @abhi23pawar 2 года назад

    Why don't we heapify?

    • @rishabhhsingh7762
      @rishabhhsingh7762 2 года назад

      heappush pushes the element to the heap and then heapifies. Without heapification, inserting an element to the heap is of no use.

    • @orellavie6233
      @orellavie6233 2 года назад +1

      @@rishabhhsingh7762 That is not actually why he is asking that. I am wondering too, because of that heap push at the start, it uses O(nlogn) instead of O(n) to build the heap. The first init should be using heapify. However, since in the worst case he is going to pop everything from the heap, it would be O(nlogn) anyways.

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

      @@orellavie6233 bruv n is 3 calm down

  • @oscargong9438
    @oscargong9438 2 года назад

    Hi, great explanation and great video. Just want to point out that string is immutable in Python, so better start the res with a list, and “”.join(res) before return.

    • @VarunMittal-viralmutant
      @VarunMittal-viralmutant 2 года назад +4

      The string concatenation works perfectly fine. No need to have list. Strings are immutable, but string concatenation will assign new memory to existing string.

  • @weiyaoli6977
    @weiyaoli6977 2 года назад

    why don't we heapify

  • @numberonep5404
    @numberonep5404 2 года назад

    Itadakimassu

  • @hieijaganshi7088
    @hieijaganshi7088 Год назад

    To be honest your solutions are usually pretty unreadable, I think readability should be prioritized over conciseness or the usage of "tricks" but that might just be me

  • @numberonep5404
    @numberonep5404 2 года назад +1

    this pb really reminds me of that quote from steve jobs "Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple.", and yep using sorting instead of heaps works just fine cause the size is just 3, i get a faster than 95% with this code :
    def longestDiverseString(self, a: int, b: int, c: int) -> str:
    mapping = [["a", a],["b", b],["c", c],]
    z = a+b+c+1
    res = []
    while z>0:
    mapping.sort(key=lambda x : x[1], reverse=True)
    t0,c0 = mapping[0]
    t1,c1 = mapping[1]
    if c0 and res[-2:]!=[t0,t0]:
    res.append(t0)
    mapping[0][1]-=1
    elif c1:
    res.append(t1)
    mapping[1][1]-=1
    z-=1
    return "".join(res)