3Sum - Leetcode 15 - 2 Pointers (Python)

  Рет қаралды 7,480

Greg Hogg

Greg Hogg

6 ай бұрын

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/gahogg/Leetcode-So...
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 KZbin 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.net/P0E3J6
My Favorite Courses:
Data Structures & Algorithms:
- UCalifornia San Diego DSA: imp.i384100.net/LP31oV
- Stanford Algorithms: imp.i384100.net/vNBoxd
- Python Data Structures: imp.i384100.net/NkZn47
- Meta Coding Interview Prep: imp.i384100.net/Y96rBJ
Python:
- UMichigan Python for Everybody: imp.i384100.net/QOLM73
- Python Mastery from MLNOW.ai: mlnow.ai/course-material/python/
- Google IT Automation w/ Python: imp.i384100.net/5g6Xyj
Web Dev / Full Stack:
- Meta Front-End Developer: imp.i384100.net/q4Jemy
- IBM Full Stack Developer: imp.i384100.net/Gj9dMn
- Meta Back-End Developer: imp.i384100.net/xkW0r5
- John Hopkins HTML, CSS & JS: imp.i384100.net/QyoRAA
- IBM DevOps: imp.i384100.net/kjd2r0
Cloud Development:
- AWS Fundamentals: imp.i384100.net/anqBjZ
- GCP Cloud Engineer: imp.i384100.net/g1jvqB
- Microsoft Azure Fundamentals: imp.i384100.net/EKm5O4
Game Development:
- Michigan State Unity Development: imp.i384100.net/6eOBnr
- UColorado C++ for Unreal Engine: www.coursera.org/specializati...
SQL & Data Science:
- SQL by MLNOW.ai: mlnow.ai/course-material/sql/
- Python for Data Science by MLNOW.ai: mlnow.ai/course-material/data...
- Google Data Analytics: imp.i384100.net/1rkWAR
- IBM Data Science: imp.i384100.net/P0ZRL6
- IBM Data Engineer: imp.i384100.net/4PbZyZ
Machine Learning & AI:
- ML Mastery at MLNOW.ai: mlnow.ai/course-material/ml/
- ML w/ Andrew Ng: www.coursera.org/specializati...
- Deep Learning w/ Andrew Ng: imp.i384100.net/a1kjJj

Пікірлер: 37
@GregHogg
@GregHogg 13 күн бұрын
Master Data Structures & Algorithms For FREE at AlgoMap.io!
@abbasfadhil1715
@abbasfadhil1715 5 ай бұрын
Naming the function three sum is wild ☠️
@zdzisawdyrman7789
@zdzisawdyrman7789 Ай бұрын
Well... everything fine apart for the face that this solutions gets: "Time limit exceeded"
@khndokarrashid2991
@khndokarrashid2991 6 ай бұрын
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 6 ай бұрын
That's awesome! Glad to hear it and best of luck :)
@jyotiaggarwal6983
@jyotiaggarwal6983 Ай бұрын
Hi, I used your solution for Python3 and after submitting,it is showing Time Limit exceeded. How can I improvise it to resolve this issue as I see we using two pointer approach here with hashMap simultaneously which costs O(N*2) complexity but sorting and storing in set increases its complexity.could you please help
@praveenchandkakarla406
@praveenchandkakarla406 Ай бұрын
Hi jyoti, You can use the below code without using hashmap, reference -> kzbin.info/www/bejne/oKu9pHpuo5eFb6M def threeSum(self, nums: List[int]) -> List[List[int]]: res=[] nums.sort() for i in range(len(nums)): if i>0 and nums[i] == nums[i-1]: continue l,r = i+1,len(nums)-1 while l < r: thSum = nums[i]+nums[l]+nums[r] if thSum < 0: l += 1 elif thSum > 0: r -= 1 else: res.append([nums[i],nums[l],nums[r]]) l += 1 while l < r and nums[l] == nums[l-1]: l += 1 return res
@arindambhattacharjee9270
@arindambhattacharjee9270 6 күн бұрын
@@praveenchandkakarla406 nai degi
@praveenchandkakarla406
@praveenchandkakarla406 6 күн бұрын
@@arindambhattacharjee9270 don't need as well
@floccinau263
@floccinau263 4 сағат бұрын
When I used this solution in Python, it was accepted. However, as you mentioned, using Python 3 causes a time limit exceed.
@ankitchoudhary4618
@ankitchoudhary4618 6 ай бұрын
Very informative 👍
@siddheshdewalekar7364
@siddheshdewalekar7364 6 ай бұрын
Nice info👍
@newbiedb
@newbiedb 4 ай бұрын
At 5:21, can you explain more about hashable and mutable in Set?
@DariusD0815
@DariusD0815 3 ай бұрын
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 3 ай бұрын
Yeah different permutations of the same numbers are not desired
@SashoSuper
@SashoSuper 6 ай бұрын
I like those videos.
@GregHogg
@GregHogg 6 ай бұрын
Awesome!
@NitinKumar-qs9tw
@NitinKumar-qs9tw 2 ай бұрын
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 2 ай бұрын
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
@samspeaks-hk1vp
@samspeaks-hk1vp 21 күн бұрын
shouldn’t time complexity more than o(n3) cause we sorting in the inner loop?
@haythemkhiari1089
@haythemkhiari1089 7 сағат бұрын
i think it is O(n^2 log(n) ) because sorting take O(log(n)) not o(n)
@ThsHunt
@ThsHunt 5 ай бұрын
why do the step of filling hashset doesnt count towards the time
@sonicrushXII
@sonicrushXII 5 ай бұрын
It does take time, But it's constant time Constants are generally dropped when Calculating end time
@lakshayjain9337
@lakshayjain9337 Ай бұрын
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
@onlywrestling7810
@onlywrestling7810 Ай бұрын
It gives TLE, only 312/313 test cases are passed
@GregHogg
@GregHogg 29 күн бұрын
Yes, sorry. Leetcode recently added a case to make this solution not work. At some point I'll make a solution for a different algorithm that works
@benravenhill484
@benravenhill484 6 ай бұрын
My brain exploded
@GregHogg
@GregHogg 6 ай бұрын
😂
@rakeshande449
@rakeshande449 5 ай бұрын
It's getting TLE
@GregHogg
@GregHogg 5 ай бұрын
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 4 ай бұрын
thanks for answered. I got TLE too.@@GregHogg
@jagrat12354
@jagrat12354 2 ай бұрын
getting Time Limit Exceeded using this method, dont know why. Even running ur code does the same
@GregHogg
@GregHogg 2 ай бұрын
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 2 ай бұрын
@@GregHogg Any idea, how to tweak the solution to pass all test cases?
@santanu_barua
@santanu_barua Ай бұрын
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); }
@antipainK
@antipainK 5 ай бұрын
Using a set feels like cheating here...
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 406 М.
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 292 М.
Опасность фирменной зарядки Apple
00:57
SuperCrastan
Рет қаралды 12 МЛН
Why Is He Unhappy…?
00:26
Alan Chikin Chow
Рет қаралды 72 МЛН
Schoolboy - Часть 2
00:12
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 7 МЛН
EVOLUTION OF ICE CREAM 😱 #shorts
00:11
Savage Vlogs
Рет қаралды 12 МЛН
Stop, Intel’s Already Dead!
13:47
Linus Tech Tips
Рет қаралды 425 М.
3 Sum | Brute -  Better - Optimal with Codes
38:25
take U forward
Рет қаралды 271 М.
The Truth About Learning Python in 2024
9:38
Internet Made Coder
Рет қаралды 167 М.
Number of Islands - Leetcode 200 - Graphs (Python)
11:01
Greg Hogg
Рет қаралды 3,6 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 634 М.
Опасность фирменной зарядки Apple
00:57
SuperCrastan
Рет қаралды 12 МЛН