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

Поделиться
HTML-код
  • Опубликовано: 6 фев 2025
  • Master Data Structures & Algorithms for FREE at AlgoMap.io/
    Code solutions in Python, Java, C++ and JS for this can be found at my GitHub repo here: github.com/gah...
    Complete DSA Pathway Zero to Hero: • Data Structures & Algo...
    Please check my playlists for free DSA problem solutions:
    • Fundamental DSA Theory
    • Array & String Questions
    • 2 Pointers Questions
    • Sliding Window Questions
    • Binary Search Questions
    • Stack Questions
    • Linked List Questions
    • Tree Questions
    • Heap Questions
    • Recursive Backtracking...
    • Graph Questions
    • Dynamic Programming (D...
    My Data Science & ML RUclips Playlist: • Greg's Path to Become ...
    Learn Python and Data Science FASTER at mlnow.ai :)
    Support the content: / @greghogg
    Follow me on Instagram: / greghogg5
    Connect with me on LinkedIn: / greghogg
    Follow me on TikTok: / greghogg5
    Coursera Plus: imp.i384100.ne...
    My Favorite Courses:
    Data Structures & Algorithms:
    UCalifornia San Diego DSA: imp.i384100.ne...
    Stanford Algorithms: imp.i384100.ne...
    Python Data Structures: imp.i384100.ne...
    Meta Coding Interview Prep: imp.i384100.ne...
    Python:
    UMichigan Python for Everybody: imp.i384100.ne...
    Python Mastery from MLNOW.ai: mlnow.ai/cours...
    Google IT Automation w/ Python: imp.i384100.ne...
    Web Dev / Full Stack:
    Meta Front-End Developer: imp.i384100.ne...
    IBM Full Stack Developer: imp.i384100.ne...
    Meta Back-End Developer: imp.i384100.ne...
    John Hopkins HTML, CSS & JS: imp.i384100.ne...
    IBM DevOps: imp.i384100.ne...
    Cloud Development:
    AWS Fundamentals: imp.i384100.ne...
    GCP Cloud Engineer: imp.i384100.ne...
    Microsoft Azure Fundamentals: imp.i384100.ne...
    Game Development:
    Michigan State Unity Development: imp.i384100.ne...
    UColorado C++ for Unreal Engine: www.coursera.o...
    SQL & Data Science:
    SQL by MLNOW.ai: mlnow.ai/cours...
    Python for Data Science by MLNOW.ai: mlnow.ai/cours...
    Google Data Analytics: imp.i384100.ne...
    IBM Data Science: imp.i384100.ne...
    IBM Data Engineer: imp.i384100.ne...
    Machine Learning & AI:
    ML Mastery at MLNOW.ai: mlnow.ai/cours...
    ML w/ Andrew Ng: www.coursera.o...
    Deep Learning w/ Andrew Ng: imp.i384100.ne...

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

  • @GregHogg
    @GregHogg  5 месяцев назад

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

  • @user-jm6gp2qc8x
    @user-jm6gp2qc8x 4 месяца назад +33

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

  • @hbhavsi
    @hbhavsi 3 месяца назад +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!

  • @christianjt7018
    @christianjt7018 6 месяцев назад

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

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

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

  • @anna-plink
    @anna-plink 6 месяцев назад

    One of my favorite solutions for a problem so far 👍

    • @GregHogg
      @GregHogg  5 месяцев назад

      Yeah it's pretty neat

  • @WatchNoah
    @WatchNoah 4 месяца назад

    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

  • @pravinprince3221
    @pravinprince3221 6 месяцев назад +1

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

    • @GregHogg
      @GregHogg  6 месяцев назад

      Glad to hear it!

  • @venkateshkannan7398
    @venkateshkannan7398 5 месяцев назад

    Best explanation👌

  • @malakggh
    @malakggh 4 месяца назад

    That was a very good explanation!

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

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

  • @sirojiddinSoftwareEngineer
    @sirojiddinSoftwareEngineer 4 месяца назад

    Good job

  • @mehdismaeili3743
    @mehdismaeili3743 6 месяцев назад

    Excellent .

  • @flynnblu6992
    @flynnblu6992 5 месяцев назад

    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 4 месяца назад

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

  • @mahmoudelazb8028
    @mahmoudelazb8028 6 месяцев назад

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

    • @TM40_AerialAce
      @TM40_AerialAce 6 месяцев назад

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

    • @mahmoudelazb8028
      @mahmoudelazb8028 6 месяцев назад +1

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

  • @thomasthedankengine5400
    @thomasthedankengine5400 6 месяцев назад

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

    • @endless9142
      @endless9142 6 месяцев назад +4

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

  • @Nechss
    @Nechss 2 месяца назад

    is using sort in the spirit of these problems?

    • @irlporygon-z6929
      @irlporygon-z6929 3 дня назад

      Yep. Any technique that produces an efficient algorithm is valid. If the challenge author wants to prevent you from sorting or doing any other specific thing they can explicitly state that it's not allowed in the problem statement. (Efficient algorithm meaning the generic technique as written out in pseudocode, as opposed to yknow. Something specific to a programming language or computer architecture.)

  • @user-jm6gp2qc8x
    @user-jm6gp2qc8x 4 месяца назад

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

  • @2EOGIY
    @2EOGIY 6 месяцев назад

    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 6 месяцев назад

      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 6 месяцев назад

      @@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 5 месяцев назад +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 5 месяцев назад

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

    • @codingcardio-nq7uc
      @codingcardio-nq7uc 22 дня назад

      why do we need to iterate through each and every element when in mid way we know we won't get any possible combination over there, breaking through just saves us lil time

  • @skinloren69
    @skinloren69 2 месяца назад

    is this nums.sorting() allowed at interviews?