whoever gave the title to this problem knew what he was doing lol
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!
One of my favorite solutions for a problem so far 👍
I liked this solution a lot more than the previous one, keep up the amazing work!
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
Thanks, for anyone interested in 2 pointer solution using SET() to store repetition in GO: NOTE 1 - WHY SORTING? Removing SORTING invalidates the logic that i++ makes the sum bigger and j-- makes the sum smaller!! If we find solution ex (-3,1,2), that means that "left" cannot be "1" anymore and "right" cannot be "2" anymore. BUT "left" CAN be "2" and "right" CAN be "1". If we sort it that basically ensures that in current run (for fixed c) this wont happen. And you can safely increment/decrement pointers. "left" will skip all 1s, and "right" will skip all 2s NOTE 2 - I know GO kindof sucks for Leetcode because there is not built in set() data structure, you have to use map. NOTE 3 - you can add logic to check if current "c" pointer is equal to previous, if it is, move to the next. But that does not influence the result speed that much import ( "sort" ) func threeSum(nums []int) [][]int { sort.Ints(nums) c := 0 i := c + 1 // left j := len(nums) - 1 //right var result [][]int var triplet [3]int uniqueTriplets := make(map[[3]int]struct{}) //storing triplets for c < len(nums)-2 { for i < j { total := nums[c] + nums[i] + nums[j] triplet = [3]int{nums[c], nums[i], nums[j]} if total == 0 { if _, exists := uniqueTriplets[triplet]; !exists { uniqueTriplets[triplet] = struct{}{} result = append(result, []int{nums[c], nums[i], nums[j]}) } i++ j-- } else if total < 0 { i++ } else { j-- } } // increment c, and reset i & j. Here you can implement "skip" logic to increment c at value different from previous (current) c++ i = c + 1 j = len(nums) - 1 } return result }
Thank you Greg for the wonderful video, it is so helpful for me and my team, thanks again
One small thing to note that, in the sorted array if we reach the number zero or a positive number we can stop the program as we know the positives don't add up to make sum as zero.
That was a very good explanation!
I'd recommend solving Two Sum and Two Sum II before approaching this problem
Best explanation👌
I do not solve this after two hour, even with help of your grafical explanation, i feel like shit. 🙁
Excellent .
Good job
What about using set instead of filtration that we do? Keep in course you'r good instructor
What about the time complexity that came from sorting? Wouldn't it be like nlog(n) times n²?
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]:
is using sort in the spirit of these problems?
greg i dont think hashmap 3sum video you've done
@GregHogg