3Sum (Updated Solution) - Leetcode 15 - Two Pointers (Python)

Поділитися
Вставка
  • Опубліковано 19 гру 2024

КОМЕНТАРІ • 31

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

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

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

    Wow, this is the clearest explanation of the solution I have come across that explains constraints with the correct example, rather than just the default example, and I am left scratching my head at the final solution, like huh, why was this needed at all? Thanks so much!

  • @user-jm6gp2qc8x
    @user-jm6gp2qc8x 3 місяці тому +17

    whoever gave the title to this problem knew what he was doing lol

  • @WatchNoah
    @WatchNoah 2 місяці тому

    instead of the two while loops in the summ == 0, you can actually also simply use a set as answer and add sorted tuples. this also has a runtime of 558ms

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

    I liked this solution a lot more than the previous one, keep up the amazing work!

    • @GregHogg
      @GregHogg  4 місяці тому +1

      Yeah this one is better. Thank you for being a paid member!!

  • @pravinprince3221
    @pravinprince3221 4 місяці тому +1

    Thank you Greg for the wonderful video, it is so helpful for me and my team, thanks again

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

      Glad to hear it!

  • @kavan3
    @kavan3 3 дні тому

    I'd recommend solving Two Sum and Two Sum II before approaching this problem

  • @anna-plink
    @anna-plink 4 місяці тому

    One of my favorite solutions for a problem so far 👍

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

      Yeah it's pretty neat

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

    That was a very good explanation!

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

    Best explanation👌

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

    is using sort in the spirit of these problems?

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

    Good job

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

    What about the time complexity that came from sorting? Wouldn't it be like nlog(n) times n²?

    • @endless9142
      @endless9142 4 місяці тому +3

      You sort only once, so its nlog(n) PLUS n^2, which simplifies to n^2

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

    What about using set instead of filtration that we do?
    Keep in course you'r good instructor

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

      Elements in an a triplet can repeat, it's just the triplets themselves that have to be unique

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

      @@TM40_AerialAce I have solved it with hash set it's a simple solutions more than this

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

    Excellent .

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

    Love your videos! But puzzled... after finding the first answer, isn't nums[hi + 1] out of range on the following, given hi starts out as (n - 1), hence we are checking nums[n]?
    while hi > lo and nums[hi] == nums[hi + 1]:

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

      Just before that part, high is decreased:
      else:
      res.append([offset, nums[low], nums[high]])
      low += 1
      high -= 1 #

  • @2EOGIY
    @2EOGIY 4 місяці тому

    8:51 What's the point of using a for loop if you do not intend to iterate through all elements?

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

      You do intend to iterate through almost all elements (length of array - 2, so you have space for the two pointers). the i in `for i in range(n)` acts as a boundary that squeezes the two pointers closer together every iteration. Sort of like the trash compactor scene from A New Hope

    • @2EOGIY
      @2EOGIY 4 місяці тому

      @@TM40_AerialAce, if there is an intent for an early exit, then a for loop is a wrong choice. Just add those conditions to a while loop.

    • @wilderclover3354
      @wilderclover3354 3 місяці тому +1

      ​@@2EOGIYi don't think there is some rule written in python documentation that u should not used for loop in those situations. Its easier to explain by using for loop than while

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

      @@2EOGIY you can use either while or for loop for any use case they are literally just syntax differences.

  • @user-jm6gp2qc8x
    @user-jm6gp2qc8x 3 місяці тому

    greg i dont think hashmap 3sum video you've done

  • @skinloren69
    @skinloren69 21 день тому

    is this nums.sorting() allowed at interviews?