3Sum - Leetcode 15 - Python

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

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

  • @NeetCode
    @NeetCode  4 года назад +111

    🚀 neetcode.io/ - I created a FREE site to make interview prep a lot easier, hope it helps! ❤

    • @alekkras3487
      @alekkras3487 4 года назад +3

      I really liked your explanation. It was very clear and clean. Keep it up and great work! :)

    • @sintuboro4224
      @sintuboro4224 4 года назад +3

      dude your doing so well. appreciate man. loved your explanation. hopeful for some constant tuts in the future

    • @vishalm784
      @vishalm784 3 года назад

      @NeetCode could you please add a video on Flattening of a LinkedList?

    • @yashrathi6862
      @yashrathi6862 3 года назад

      Please put explanation only if not the code of as many as you could. You are an absolute life saver.

    • @yashrathi6862
      @yashrathi6862 3 года назад

      These videos are amazing! Luckily I found this channel.

  • @mohammadusman9975
    @mohammadusman9975 4 года назад +334

    I will literally pay for leetcode premium if intuitive video explanations like these were supplied with it

  • @ismbks
    @ismbks 7 месяцев назад +202

    who taught neetcode how to leetcode if there was no neetcode back then 🤔

    • @servantofthelord8147
      @servantofthelord8147 4 месяца назад +42

      Who delivers mail to the mailman? Who coaches the coach? These questions also keep me up at night.

    • @diasutsman
      @diasutsman 3 месяца назад +4

      God

    • @kleingames4626
      @kleingames4626 3 месяца назад +4

      @@servantofthelord8147
      ​​
      dude , in the bible it says Jesus didn’t know hour .
      It means he is not G .
      It is a corrupt religion with a wrong belief système .
      Islam is the only correct religion that doesn’t have contradictions in it

    • @kleingames4626
      @kleingames4626 3 месяца назад

      @@diasutsman
      Contradiction #1: How did Judas Iscariot die?
      Matthew 27:5
      Judas hung himself out of guilt.
      Acts 1:18
      Judas fell and "his body burst open".
      Contradiction #2: Who was the father of Joseph, husband of Mary?
      Matthew 1:16
      Joseph's father was Jacob.
      Luke 3:23
      Joseph's father was Heli.
      Contradiction #3: Did Peter preach to Jews or
      Gentiles?
      Matthew 10:5-6
      Peter (and all the disciples) were told to avoid
      Gentiles, and preach only to Jews.
      Acts 15:7
      Peter was told "some time ago" that he should preach to Gentiles.
      Contradiction #4: Are we saved by faith or works?
      Matthew 7:21
      Faith is not enough; obedience to the Father is also necessary to enter heaven.
      John 3:16
      Anybody who believes in Christianity "shall not perish but have eternal life"
      Contradiction #5: Should good deeds be seen?
      Matthew 5:16
      Good deeds should be seen.
      Matthew 6:1-4
      We must not "practice our righteousness in front others".
      Contradiction #6: According to Paul, is the law necessary or not?
      Romans 3:31
      The law is to be upheld.
      Romans 6:14
      Christians are no longer under the law, but "under grace".
      Contradiction #7: How many believers were there at the time of the ascension?
      Acts 1:15
      There were about 120 believers.
      1 Corinthians 15:6
      There were more than 500 believers.
      Contradiction #8: Who went to the tomb after the crucifixion?
      John 20:1
      Only Mary Magdalene went to the tomb.
      Matthew 28:1
      Mary Magdalene and "the other Mary" went to thetomb.
      Mark 16:1
      The two Mary's and Salome went to the tomb.
      Luke 24:10
      Mary Magdalene, Joanna, Mary, mother of James, and "others with them" told the disciples about the tomb.
      Contradiction #9: Is righteousness possible, and can it lead to salvation?
      Matthew 25:46
      The righteous will have eternal life.
      Romans 3:10
      There is "no one righteous, not even one"
      Contradiction #10: When did Jesus ascend?
      Luke 24:21
      The third day after the crucifixion
      Acts 1:3
      Jesus remained among the disciples for forty days.

    • @sciencecosmos5126
      @sciencecosmos5126 3 месяца назад +4

      Islam teaches earth is flat

  • @valentinmorales2633
    @valentinmorales2633 3 года назад +36

    At the beginning I was hesitant of watching this video because I'm not really proficient in Python, but the explanation of the problem was super clear and the code is so easy to understand. Good job!

  • @More-right-rudder
    @More-right-rudder 3 года назад +30

    So, one optimization I can think of is if the i value ever becomes positive then we can break out of the loop. Sino in the sorted list the if i is positive then adding the rest of the two value will always make the sum > 0. Of course this would only work when we are trying to find a, b and c such that a + b + c = 0.

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

      b and c can be negative tho

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

      @@Philgob No it can't since we are sorting the array before the loop

  • @htoomyatnaing5775
    @htoomyatnaing5775 2 года назад +4

    Just wanted to drop an appreciation comment that your explanations and videos are better than services that charge hundreds of dollars. I used to not enjoy Leetcoding because I'd get frustrated when I try to read the solutions and don't understand them, but now I do thanks to Neetcode!

  • @edwardsolomon643
    @edwardsolomon643 Год назад +1

    we can optimize the a loop by adding if a > 0: break #because as the leftmost pointer in a sorted array, if a > 0, that means we cant possibly adding left and right to 0

  • @Eric-fv8ft
    @Eric-fv8ft 3 года назад +3

    Great explanation on how to avoid duplicate count for two sums!!

  • @TheBeansChopper
    @TheBeansChopper 10 месяцев назад +2

    Tbh I think the jump towards immediately sorting the array is a bit unintuitive for beginners. Having watched the previous videos I would've started thinking "we can use hash sets" to check if for a,b,c we have already seen the number. And then the question would be how to improve runtime

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

    the way you explained three_sum is nothing but a number plus two_sum simplified it. you are super awesome!!

  • @steveh7922
    @steveh7922 Год назад +1

    Thank you, 100% right about it being easier if doing Two Sum first
    I didn't and what I created was...unique

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

    the way you teach its so chill and calm feels like you are really understanding the question and also building our logic building thanks a lot

  • @georgiotarabay3281
    @georgiotarabay3281 Год назад +1

    I think the space complexity part is wrong there's no way it is O(1) because we still have the res list. Correct me if I am wrong but I would argue the space complexity is O(N) because we have the worst case where each element is unique and we'll len(res)==len(nums)

    • @dnm9931
      @dnm9931 7 месяцев назад

      Exactly what I was thinking, we always need extra space to store the answer

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

    this exact code and I got Wrong Answer for [0,0,0,] input, and then TLE when I added a special case for it. Maybe they've added new test cases.

  • @AesEncryption
    @AesEncryption 7 месяцев назад

    I've done like a million of stupid mistakes even after watching and understanding the video. It took me a day ahah now is engraved in my memory

  • @auto8ot644
    @auto8ot644 3 года назад +1

    I saw a couple videos of 3sum and couldn't get it until I saw your explanation. Now I'm subscribed :)

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

    After watching many of your videos for many months, it's jarring to see a one-off where you're using white background.
    Nowadays so much prefer having a darker themed editor (after long time using light colors).
    Somehow I find it easier to read code on a dark background.

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

    Even when I solved Two Sum and Two Sum II, I still couldn't figure out how to fix a value from the index and do the logic. Your explanationation is really helpful

  • @abhinavl999
    @abhinavl999 7 месяцев назад

    Thanks for the solution! It really helped me to understand. I would also suggest to add the condition where the first name (a) is greater than zero. If it is, than just break the loop as there will be no solution afterwards.

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

    Actually you do increment left and decrement right in the else statement. Just incrementing left works but doing both would find the solution faster

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

    you can also use a set instead of a list in order to prevent duplicates that appear from elements that have the same value and are one near the other.. but this is still executing more instructions..

  • @pengtroll6247
    @pengtroll6247 3 года назад +3

    Hey, man great explanation! I am just confused on one part: I believe there are quadratic distinct pairs that would equal the target sum.
    For instance: [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4], targetSum=0
    The result array would be [[-5, -4, 4], [-5, -3, 3], [-5, -2, 2], [-5, -1, 1],
    [-4, -3, 3], [-4, -2, 2], [-4, -1, 1],
    [-3, -2, 2], [-3, -1, 1],
    [-2, -1, 1],
    [-1, 0, 1]]
    There would be n^2/2 combinations, so I believe the space complexity is O(n^2).
    Could you please clarify if this is correct?

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

      There are n(n+1)/2 possible pairs. For each element in the array, you pick another pair to form a triplet so the limiting factor is O(n^3)

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

    These are very helpful in my prep for my google loop! Thanks a lot.

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

    Your explanations are what I strive towards! Keep up the good work man!

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

    Crisp and intuitive explanation btw I was hoping for runtime error at the end.

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

    your code looks very neat. Mine looks very messy. when you started doing problems were your code messy too? how did you get to keep it so simplicit

  • @arandomytname
    @arandomytname 3 года назад +1

    Maybe a dumb question: would you mind explaining why we still need to update the "l" pointer after L18? L18 appends the combo of 3 numbers whose sum equals 0 to the result list, and so why do we need L19 to L21? Thanks.

    • @hurfyd
      @hurfyd 3 года назад +4

      if you have a case like: [-1, 0, 0, 1, 1] there is a solution at [-1, 0, 1] we now carry on in the while loop and increment the left and decrement the right. this will give us a duplicate solution of [-1, 0, 1] again. after finding a solution you have to keep moving the pointers until you get to the next non-duplicated number in the array

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

    Your tutorials help a lot.. Thankyou!!

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

    Very comprehensive, thank you so much

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

    why didnt u used set() to remove the duplicates? , directly removing the duplicayes in start will make the code easy and short

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

    I am confused on the last 3 lines of code before the return statement. Why do we need to increment the left pointer by one?

  • @Zxs2202
    @Zxs2202 11 дней назад

    3sum is my favorite sum

  • @karann6010
    @karann6010 3 года назад

    Keep up the good work love bro

  • @eliyoung9406
    @eliyoung9406 9 месяцев назад

    I mean I found a brute force algorithm that just checks if the current solution is a duplicate and doesnt add it if it is. But of course I get time limit exceeded because you always have to find the most efficient algorithm smfh.

  • @LamNguyen-nm1id
    @LamNguyen-nm1id Год назад

    came to say during 2022 i found a posted solution on LC using HashSet which helps with checking duplicate, that helps a ton without checking for duplication

  • @trhoangdat
    @trhoangdat 10 месяцев назад

    thank you!

  • @edwardteach2
    @edwardteach2 3 года назад

    U a God.. I agree the hardest part of incrementing that left pointer when your 3sum == 0. Else you'll run into an infinite loop or get multiple duplicate pairs.

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

    Really good explanation

  • @ShivangiSingh-wc3gk
    @ShivangiSingh-wc3gk 2 года назад

    You do an awesome job!!!

  • @ravikant-hi8mz
    @ravikant-hi8mz 3 года назад +1

    ok, so I have tried the exact same code in leetcode and it is not going through. Any ideas?
    class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
    l=len(nums)
    res=[]
    nums.sort()
    for i, a in enumerate(nums):
    if i>0 and a==nums[i-1]:
    continue
    l, r=i+1, l-1
    while l0:
    r-=1
    elif threeSum

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

    Output {-1,-1,2} same elements, you saying same element we skips

  • @spombg
    @spombg 9 месяцев назад

    Ok so this is basically TwoSum II inside of TwoSum, right?

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

    could we do this for optimization - if nums[i] > 0:
    return result

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

    Is using sort() right way to do a problem using pre defined library? , I'm new please don't mind if this is a dumb question.....

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

    best explatation ever

  • @mohithadiyal6083
    @mohithadiyal6083 3 года назад

    You are amazing. 👍🏻

  • @Riteshyadav-vp7db
    @Riteshyadav-vp7db Год назад

    You'r Awesome

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

    I feel hashmap would be a better solution

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

    Why didn't we solve it like the 'longest palindrome substring' problem?

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

    Will this code work in an array that has the values [-2, -2,3,4] Since i = 0 which is -2, L = -2 and R = 4 which threesum = 0 but -2 is used twice? How is this solved?

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

    I think here ruclips.net/video/jzZsG8n2R9A/видео.html explanation is a bit wrong since we still can add L=-3 because it is not considered as duplicate if [-3,-3,x]. All other staff is excellent. Thanks!

  • @FitnessChaos
    @FitnessChaos 3 года назад

    Nice solution

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

    Can anyone please explain this to me, my doubt is if i miss to increment my start pointer after finding a triplet, the program gives memory limit exceed error.
    I don't get it why, it should not run in infinite loop as we are also checking start < end.
    This is the full code :
    vector threeSum(vector& nums) {
    sort(nums.begin(),nums.end());
    vector ans;
    vector triplet(3);
    for(int i=0;i 0 && nums[i] == nums[i-1])continue;
    int start = i+1;
    int end = nums.size()-1;
    while(start < end)
    {
    int sum = nums[i] + nums[start] + nums[end];
    if(sum > 0 )
    {
    --end;
    }
    else if(sum < 0)
    {
    ++start;
    }
    else
    {
    triplet[0] = nums[i];
    triplet[1] = nums[start];
    triplet[2] = nums[end];
    ans.push_back(triplet);
    ++start;
    while(nums[start] == nums[start-1] && start < end)++start;
    }
    }
    }
    return ans;
    }

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

    Thanks man

  • @niko-l-
    @niko-l- 3 года назад +568

    Man, you do explanations exceptionally clean. Please don't stop.

    • @NeetCode
      @NeetCode  3 года назад +41

      Thanks, appreciate the kind words!

    • @yt-sh
      @yt-sh 2 года назад +3

      @@NeetCode Hey man you got any tips to learn leetcode faster?

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

      Yeah man @NeetCode, got any tips to learn leetcode faster

    • @deepanshuchoudhary4598
      @deepanshuchoudhary4598 2 года назад +7

      While the problem name is 'threesome' and you're saying "don't stop". I can't resist a cracked laugh.

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

      @@deepanshuchoudhary4598 hehe

  • @ishtiaquehussain
    @ishtiaquehussain 2 года назад +302

    Your last comment that it's a pretty hard problem and showing that you struggled was encouraging. I did both the Two Sum and Two Sum II problems but still was struggling in figuring out the solution. Thanks a lot for such a nice explanation and honest feedback! Appreciate it!

    • @stefano_schmidt
      @stefano_schmidt Год назад +1

      hahah, same)

    • @aaqibjavedz2569
      @aaqibjavedz2569 Год назад +14

      I had done both 2 sums, but struggled on this problem. I feel that this should’ve been a leetcode 'hard' problem instead.

    • @juliramoos
      @juliramoos 10 месяцев назад

      same!

    • @AdityaSingh-wr7ow
      @AdityaSingh-wr7ow 7 месяцев назад

      same here bro haha

    • @ngndnd
      @ngndnd 7 месяцев назад +2

      im ngl i didnt understand the question at all lmao

  • @rostm3808
    @rostm3808 4 года назад +116

    I can see this channel blowing up. just keep making vids man, these are great explanations! ty

  • @Miniay99
    @Miniay99 Год назад +45

    more than anything else i love how patient you are with us as the viewer, never skipping a step, sayng something is obvious, and offering encouragement by showing us all the times you screwed up too. it really helps. invaluable resource!!

  • @PrashanthiRamachandran
    @PrashanthiRamachandran Год назад +11

    Hi! Maybe I'm missing something, but what about the case where you have an array like [-3,-3,0,3,6]. In this case, both [-3,-3,6] and [-3,0,3] are valid. If you skip the second -3, you will miss out on [-3,0,3] as one of the triplets.

    • @ArjunAJ-hy4tv
      @ArjunAJ-hy4tv Год назад +2

      No, it won't miss that pair. Notice the inner logic runs for every item of the list via outer for loop. I tested it and works as expected.

    • @venkatanishanthchaganty1697
      @venkatanishanthchaganty1697 Год назад +3

      You bring up a good question that I spent some time thinking about. For the case you mentioned. [-3,-3,6] gets included when a is on the first -3, l is on second -3 and r is on 6. The l does not incremented before a solution is added because there are no checks to see if a and nums[l] are duplicates. The second -3 won't be skipped because when the sum of a + l + r is 0, the left pointer is incremented. So l comes to 0 while as 'a' stays on the first -3. Eventually r will end up on 3, and we will have [-3,0,3] as another solution. Hope it is useful to anyone who has the same doubt!

  • @sujithkumar1997
    @sujithkumar1997 2 года назад +21

    As a guy who is struggling with the easiest of LC questions, the final part of this video is really motivating.

  • @eecs482
    @eecs482 2 года назад +55

    after sorting, you could check to see if the first element is greater than 0. if it is, then you can just return before looping. there's no possible triplet if the first number is greater than 0 (after sorting). if you want to add this optimization, you have to check to make sure it's not empty before trying to access first element

  • @Cloud-577
    @Cloud-577 3 года назад +16

    rather than just solving the question, you helped us understand the question and how to go about solving it, and THAT is what many channels lack! Thank you so much

  • @AryanGorwade-e9f
    @AryanGorwade-e9f 2 месяца назад +1

    Why can't you use three pointers instead? Also why does this approach work for sums adding up to 0 but not for any other number?

  • @meltygear5955
    @meltygear5955 Год назад +2

    How is this a medium? My brain hurts 😭🧠

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

    The following code results in wrong aswer on leet code
    class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:

    res = []
    nums.sort()
    if len(nums) < 3:
    return []

    for i in range(len(nums)):
    if i > 0 and nums[i-1] == nums[i]:
    continue

    l = i
    r = len(nums)-1

    while l < r:
    zero = nums[i] + nums[l] + nums[r]
    if zero > 0:
    r -= 1
    elif zero < 0:
    l += 1
    else:
    res.append([nums[i], nums[l], nums[r]])
    l += 1
    while nums[l] == nums[l-1] and l < r:
    l += 1

    return res

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

    The whole software engineer hiring process is broken. These coding questions are just ridiculous, have nothing to do with your day to day job and don't give any clue about whether you are or you are not a capable and reliable programmer in a dev team.

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

      Which keyboard you are using?

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

    Comment for the algo, but also just appreciate the encouragement. I feel like an idiot trying these problems, and I appreciate the reassurance that I'm probably not, it's just a hard problem to solve

  • @Dexter-lw4xv
    @Dexter-lw4xv Год назад +4

    Thanks bro, I was frustrated because I was not able to solve it, so I got high, and I was high and frustrated, but now with your video I understand the solution, so now I'm high and happy, thanks a lot :)

  • @NirmalSilwal
    @NirmalSilwal Год назад +1

    Java solution -
    public List threeSum(int[] nums) {
    Set result = new HashSet();
    Arrays.sort(nums); // easier to ignore duplicates and deduce to two sum II problem
    for (int i = 0; i < nums.length - 1; i++) {
    // to handle duplicates
    if (i > 0 && nums[i] == nums[i - 1]) {
    continue;
    }
    // now 2 elements after ith index needs to be identified like two sum II problem
    int target = -nums[i];
    List pairs = new ArrayList();
    // two pointer approach for sorted array to find two sum
    int low = i + 1, high = nums.length - 1;
    while (low < high) {
    if (nums[low] + nums[high] == target) {
    pairs.add(nums[i]);
    pairs.add(nums[low]);
    pairs.add(nums[high]);
    low++;
    high--;
    result.add(new ArrayList(pairs));
    pairs = new ArrayList();
    }
    else if (nums[low] + nums[high] < target) low++;
    else if (nums[low] + nums[high] > target) high--;
    }
    }
    return new ArrayList(result);
    }

  • @OM-el6oy
    @OM-el6oy 3 года назад +7

    thanks for directing me to 2sum part 2. I was indeed able to immediately solve 3sum after i solved that one with two converging pointers.

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

    Thanks!

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

      Thank you so much!

  • @cristianbedoya4623
    @cristianbedoya4623 2 года назад +7

    I have to say, there are few channels I will like + subscribe + hit notification bell, and you are one of them! Currently looking at practicing for interviews and this is incredibly helpful! Thank you for doing what you do!

  • @phlix1
    @phlix1 7 месяцев назад +2

    This is the only 3some a programmer will ever get.

  • @benstone5303
    @benstone5303 9 месяцев назад +1

    I'm confused how the first conditional doesn't prevent something like [-7,-7,0,3,4,7] from passing where there are two solutions as shown: [[-7,0,7], [-7,3,4]]. Wouldn't sorting the input array cause the second -7 in the input Array to hit the first conditional, skipping the l/r pointer logic completely? Then the loop resumes on 0 and l takes the value of 3. And l can only increment, not decrement. Am I overlooking something obvious?

    • @ShashankSharma-ul5hi
      @ShashankSharma-ul5hi 17 дней назад

      The first loop would stop at -7, the left pointer would be pointing at 0.

  • @AG-cx1ug
    @AG-cx1ug 6 месяцев назад +2

    what if the list was -3 -3 1 1 2 2 3 4 and our a in a loop was at -3; wouldn't it make two combinations -3 1 2, and then again -3 1 2 because we're only checking for the repition of a?

  • @christianmorabito4148
    @christianmorabito4148 Год назад +1

    Why is the first 'for loop' condition:
    if i > 0 and nums[i] == nums[i-1]:
    Instead of just:
    if nums[i] == nums[i-1]:
    ??
    If the list is sorted, then nums[0] != nums[-1] anyway, so the condition feels redundant. Am I missing something?
    Thanks :)

    • @Iam-IslamAshraf
      @Iam-IslamAshraf 3 месяца назад

      In many programming languages there is no negative indices so if I used nums[-1] it will give me an error

  • @conde3915
    @conde3915 Год назад +1

    Why do you not need a while loop to move right pointer as long as your current right is equal to the last right pointer?

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

      this is the question i got. Did you figure it out?

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

      ok there is actually an explanation right in the video but i just didn't comprehend it at first, it starts at 10:00

  • @supremoluminary
    @supremoluminary Год назад +1

    [NeetCode] What if our left pointer was here: [points to -2 at index 0]
    # [-2, -2, 0, 2, 2]
    That can't happen. l's lowest value is i + 1.

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

      exactly what i was wondering, that last bit about the left pointer isnt quite clear to me

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

    why not use set instead of list. using set makes it easier

  • @srinadhp
    @srinadhp 3 года назад +5

    As someone already said, Please dont stop. Keep up the good work. This is another gem of explanation and clean solution of yours. Hats off. Loved the way how you showed all your previous failed attempts.

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

    crazy how I have the exact same code line by line in JS and not passing all test cases

    • @georgivdimitrov
      @georgivdimitrov Год назад +2

      The reason is that in JS the .sort() does not work as expected, try adding .sort((a, b) => a - b) and it should work.
      Explanation:
      Calling .sort() does the following [-1, -2, -3, 0, 1, 2, 3].
      In order to have [-3, -2, -1, 0, 1, 2, 3] you need to pass a comparator function.

  • @Jack-yu6ze
    @Jack-yu6ze 18 дней назад

    At minute 11:00 you are trying to explain why we only need to move the left pointer but your explanation is only valid for the current example array you are showing. What if there are more negative values in the list. Would have appreciated if you explained that for example [-2, -2, -1, 0, 0, 1, 1, 2, 2]

  • @SkipperLimeblue
    @SkipperLimeblue Год назад +1

    3Sum.... Mmmmmm
    Really interesting question

  • @Icantthinkofanameman
    @Icantthinkofanameman 24 дня назад

    You recommended doing Two Sum II first before 3Sum but in neetcode blind75 there is no Two Sum II only in the 150. Was it just to make space for other problems? Assuming something had to get cut out

  • @alexanderp7521
    @alexanderp7521 16 дней назад

    This line `if i > 0 and a == nums[i-1]: continue` gives huge optimization, it literally went from `beats 5%` to `beats 60%`

  • @suvajitchakrabarty
    @suvajitchakrabarty 3 года назад +5

    The explanation for the code solution was amazing!

  • @_dion_
    @_dion_ 10 месяцев назад +1

    excellent explanation, but the problem was hard i must say - at least for me!

  • @dmytro.pyvovarenko
    @dmytro.pyvovarenko 8 месяцев назад

    I guess the author is not fully right about the duplication concept.
    The idea is not having the same value on the same position: [-3, 3, 2, 1, 0]:
    There could be 2 options with the same -3 at index 0:
    [-3, 3, 0] & [-3, 2, 1].
    Thus, problems in the tuples overall, like the entire tuple must be unique regardless of the position.
    Anyway, good examination.

  • @fahimmuntasirniloy
    @fahimmuntasirniloy Год назад +1

    man i love you

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

    Hello man, thanks a lot for sharing this high-quality explanation. One question on my mind if you don't mind me asking, how can I compute the space complexity of output `res`? Thanks again!

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

    Imagine you're in an interview in 2000, and you don't know this technique exists. What are the odds you're going to come up with it on the spot? I think only a 0.0000001% genius would be able to.

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

      back then interviews were way easier too, I bet. Many times pretty much if you have a decent CS degree, you are hired.

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

    I'm trying to understand in the else statement in the Github optimized solution after result.append, we increment the left pointer by 1 and decrement the right pointer by 1.
    In this video, he left out r -= 1 but added it to the optimized solution on github.
    I'm trying to understand why the solution is more optimized with this r -= 1?

  • @olzhasarystanov9582
    @olzhasarystanov9582 Год назад +7

    I was actually just looking for an adult video, but ended up landing a job at Google instead

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

    I disagree with the -3 not allowed to be duplicated, it's clearly seen in the example that -1 is twice, and also what if you had a 6 somewhere there in your example and you needed -3 -3 + 6 = 0

  • @rjlacanlaled9419
    @rjlacanlaled9419 10 месяцев назад

    Do I need to just keep on trying out problems until I can solve them by finding similar patterns? I can never come up with algorithm by myself, I always check out tutorials and it's frustrating af.

  • @pigna8647
    @pigna8647 3 года назад +1

    Not work with input like [-1,0,1,2,-1,-4,-2,-3,3,0,4]

  • @vishnugajulapalli5305
    @vishnugajulapalli5305 3 года назад +3

    Dude, you are just killing it, by the way, what is the blackboard you are using to illustrate the concept

    • @NeetCode
      @NeetCode  3 года назад +1

      Thanks, I appreciate it! I'm using Paint 3D, i think it's free for anyone with windows 10.

    • @vishnugajulapalli5305
      @vishnugajulapalli5305 3 года назад +1

      @@NeetCode Ok, thanks

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

    my only issue with this Video it I can only like it this Once.... Lol...
    More important than the solution was the evidence at the end showing all the work you put in for this. Pure HOPE dude.
    Thank you.

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

    Wow in 2024 there were 6k thump ups and 2024 we've jumped to 31k, just an oberservation

  • @rahulbawa6864
    @rahulbawa6864 3 года назад +1

    ruclips.net/video/jzZsG8n2R9A/видео.html
    This is the AHA! moment for me. You skip the adjacent duplicate element because in the previous loop you would have already tried finding a pair for it.