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!
Naming the function three sum is wild ☠️
Love your videos man! im currently a data analyst trying to transfer over to software engineering and this is helping me a lot!
That's awesome! Glad to hear it and best of luck :)
Very informative 👍
Nice info👍
At 5:21, can you explain more about hashable and mutable in Set?
What is considered a "duplicate triplet"? All value permutations of a triplet are duplicates? e.g. [2,-1,-1] or [-1,2,-1]?
Yeah different permutations of the same numbers are not desired
I like those videos.
Awesome!
why do the step of filling hashset doesnt count towards the time
It does take time, But it's constant time
Constants are generally dropped when Calculating end time
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
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);
}
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.
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
My brain exploded
😂
It's getting TLE
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
thanks for answered. I got TLE too.@@GregHogg
getting Time Limit Exceeded using this method, dont know why. Even running ur code does the same
They recently changed this question so this solution doesn't run on the last test case. Very dumb of them to do that
@@GregHogg Any idea, how to tweak the solution to pass all test cases?
Using a set feels like cheating here...