REORGANIZE STRING (Leetcode) - Code & Whiteboard

แชร์
ฝัง
  • เผยแพร่เมื่อ 4 ม.ค. 2025

ความคิดเห็น • 13

  • @martialstudios4474
    @martialstudios4474 4 ปีที่แล้ว +1

    Thanks for walking us through the thought process. Great vid. I want to point out one thing though, your code works but not exactly in the way you're thinking ... your charCountsIdx is a simple integer which is immutable in python. After your first createString call, your charCountsIdx value is back to 0 when you call createString the second time.

    • @babybear-hq9yd
      @babybear-hq9yd  4 ปีที่แล้ว +1

      Damn, you really do never stop learning! I didn't realize that in fact it was resetting back to 0, and I guess subsequently jumping back through all the characters until it reached the first occurrence of a non-zero-frequency character in the charCounts array.
      Thanks a lot for the correction. I'd like to pin this comment so people don't make the same mistake that I did :)

    • @martialstudios4474
      @martialstudios4474 4 ปีที่แล้ว +2

      @@babybear-hq9yd seriously bro, it's a constant cycle of learning and losing sleep and hair in the process! i meant to post my regurgitation of your logic as well:
      def reorganizeString(self, S: str) -> str:
      charfreq = collections.Counter(S)
      n = len(S)

      if max(charfreq.values()) > (n+1)/2:
      return ""

      freqlist = list(charfreq.items())
      freqlist.sort(key = lambda x: x[1], reverse=True)
      result = [""] * n
      writeidx = 0

      for char, freq in freqlist:
      for i in range(freq):
      result[writeidx] = char
      writeidx += 2

      if writeidx >= n:
      writeidx = 1

      return "".join(result)

  • @sunginjung3854
    @sunginjung3854 3 ปีที่แล้ว

    Please keep making videos. I really appreciate all your explanations. They are great!

    • @babybear-hq9yd
      @babybear-hq9yd  3 ปีที่แล้ว

      thanks a lot :) so happy they help!

  • @adityasaraf1782
    @adityasaraf1782 2 ปีที่แล้ว

    amazingly explained brother

  • @alifyamohamedali1002
    @alifyamohamedali1002 3 ปีที่แล้ว

    Thanks so much for walking us through. I have a question in regards to this, why not use a maxheap to build the string?

  • @toekneema
    @toekneema 4 ปีที่แล้ว

    This is exactly the same as distant barcodes right?

    • @babybear-hq9yd
      @babybear-hq9yd  4 ปีที่แล้ว +1

      YES! That's the one, thank you! I knew I'd done a similar one but couldn't remember which one it was for the life in me. Hahah!

    • @toekneema
      @toekneema 4 ปีที่แล้ว

      @@babybear-hq9yd hahaha yep I watched your distant barcodes video like 2 weeks ago and commented on it as well lol

    • @nikhilkarle4948
      @nikhilkarle4948 3 ปีที่แล้ว

      @@babybear-hq9yd this is same as the Task Scheduler problem too

  • @gyanp3701
    @gyanp3701 4 ปีที่แล้ว

    Hello i need your help i want to ask you about a youtuber ( Amit Sengupta) how he make his video i mean which software or coding he is using to make these type of explanation video please help me.