3Sum - Leetcode 15 - 2 Pointers (Python)

Поделиться
HTML-код
  • Опубликовано: 23 янв 2024
  • The Python code for this problem can be found at my GitHub repo here: github.com/gahogg/Leetcode-So...
    Please check my playlists for free DSA problem solutions:
    • 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...
    Learn Python and Data Science at mlnow.ai :)
    Best Courses for Analytics:
    ---------------------------------------------------------------------------------------------------------
    + IBM Data Science (Python): bit.ly/3Rn00ZA
    + Google Analytics (R): bit.ly/3cPikLQ
    + SQL Basics: bit.ly/3Bd9nFu
    Best Courses for Programming:
    ---------------------------------------------------------------------------------------------------------
    + Data Science in R: bit.ly/3RhvfFp
    + Python for Everybody: bit.ly/3ARQ1Ei
    + Data Structures & Algorithms: bit.ly/3CYR6wR
    Best Courses for Machine Learning:
    ---------------------------------------------------------------------------------------------------------
    + Math Prerequisites: bit.ly/3ASUtTi
    + Machine Learning: bit.ly/3d1QATT
    + Deep Learning: bit.ly/3KPfint
    + ML Ops: bit.ly/3AWRrxE
    Best Courses for Statistics:
    ---------------------------------------------------------------------------------------------------------
    + Introduction to Statistics: bit.ly/3QkEgvM
    + Statistics with Python: bit.ly/3BfwejF
    + Statistics with R: bit.ly/3QkicBJ
    Best Courses for Big Data:
    ---------------------------------------------------------------------------------------------------------
    + Google Cloud Data Engineering: bit.ly/3RjHJw6
    + AWS Data Science: bit.ly/3TKnoBS
    + Big Data Specialization: bit.ly/3ANqSut
    More Courses:
    ---------------------------------------------------------------------------------------------------------
    + Tableau: bit.ly/3q966AN
    + Excel: bit.ly/3RBxind
    + Computer Vision: bit.ly/3esxVS5
    + Natural Language Processing: bit.ly/3edXAgW
    + IBM Dev Ops: bit.ly/3RlVKt2
    + IBM Full Stack Cloud: bit.ly/3x0pOm6
    + Object Oriented Programming (Java): bit.ly/3Bfjn0K
    + TensorFlow Advanced Techniques: bit.ly/3BePQV2
    + TensorFlow Data and Deployment: bit.ly/3BbC5Xb
    + Generative Adversarial Networks / GANs (PyTorch): bit.ly/3RHQiRj
    Become a Member of the Channel! bit.ly/3oOMrVH
    Follow me on LinkedIn! / greghogg
    Full Disclosure:
    Please note that I may earn a commission for purchases made at the above sites! I strongly believe in the material provided; I only recommend what I truly think is great. If you do choose to make purchases through these links; thank you for supporting the channel, it helps me make more free content like this!

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

  • @abbasfadhil1715
    @abbasfadhil1715 4 месяца назад +14

    Naming the function three sum is wild ☠️

  • @khndokarrashid2991
    @khndokarrashid2991 5 месяцев назад +3

    Love your videos man! im currently a data analyst trying to transfer over to software engineering and this is helping me a lot!

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

      That's awesome! Glad to hear it and best of luck :)

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

    Very informative 👍

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

    Nice info👍

  • @newbiedb
    @newbiedb 3 месяца назад +1

    At 5:21, can you explain more about hashable and mutable in Set?

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

    What is considered a "duplicate triplet"? All value permutations of a triplet are duplicates? e.g. [2,-1,-1] or [-1,2,-1]?

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

      Yeah different permutations of the same numbers are not desired

  • @SashoSuper
    @SashoSuper 5 месяцев назад +1

    I like those videos.

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

    why do the step of filling hashset doesnt count towards the time

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

      It does take time, But it's constant time
      Constants are generally dropped when Calculating end time

  • @lakshayjain9337
    @lakshayjain9337 15 дней назад

    Great logic
    But why this c++ code is storing similar strings
    vector threeSum(vector& nums) {
    unordered_map map;
    int i,j,n=nums.size();
    vector ans;
    sort(nums.begin(),nums.end());
    for(i=0;i

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

    Java Solution for this:
    -------------------------------------
    public List threeSum(int[] nums) {
    Map map = new HashMap();
    for (int i = 0; i< nums.length; i++) {
    map.put(nums[i], i);
    }
    Set result = new HashSet();
    for (int i = 0; i< nums.length;i++) {
    for (int j = i+1; j< nums.length; j++) {
    int desired = -nums[i] - nums[j];
    if (map.containsKey(desired) && map.get(desired)!= i && map.get(desired)!=j) {
    List triplet = Arrays.asList(nums[i], nums[j], desired);
    Collections.sort(triplet);
    result.add(triplet);
    }
    }
    }
    return new ArrayList(result);
    }

  • @NitinKumar-qs9tw
    @NitinKumar-qs9tw 28 дней назад

    Hey Greg! unfortunately time limit exceeded for last test case [0,0,0,0,0.........0,0,0] on Leetcode. 312/313 test cases passed.

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

      Yeah, they added this test case in literally a few days after I posted this video. I'll have to redo it with the n^2 solution that avoids using a Hashmap

  • @benravenhill484
    @benravenhill484 5 месяцев назад +2

    My brain exploded

  • @rakeshande449
    @rakeshande449 4 месяца назад +1

    It's getting TLE

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

      Yes, they actually changed it very recently and this no longer passes! You need to do the one where you sort and then use two pointers now

    • @maestro.a
      @maestro.a 3 месяца назад

      thanks for answered. I got TLE too.@@GregHogg

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

    getting Time Limit Exceeded using this method, dont know why. Even running ur code does the same

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

      They recently changed this question so this solution doesn't run on the last test case. Very dumb of them to do that

    • @saaddude1
      @saaddude1 29 дней назад

      @@GregHogg Any idea, how to tweak the solution to pass all test cases?

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

    Using a set feels like cheating here...