-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreeSum.py
More file actions
55 lines (43 loc) · 1.99 KB
/
threeSum.py
File metadata and controls
55 lines (43 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def threeSum(nums):
# Step 1: Sort the array to use the two-pointer approach efficiently
nums.sort()
result = [] # This will store the unique triplets
n = len(nums)
# Step 2: Iterate through the array, fixing one element at a time
for i in range(n):
# Step 3: Skip duplicate elements to avoid repeating triplets
if i > 0 and nums[i] == nums[i - 1]:
continue # Skip the same number we've already used as a starting point
# Step 4: Set up two pointers
left = i + 1 # Start just after the current index
right = n - 1 # Start at the end of the array
# Step 5: While left pointer is before the right
while left < right:
total = nums[i] + nums[left] + nums[right] # Calculate the sum of the triplet
if total < 0:
# Step 6: If the sum is too small, move left pointer right to increase the sum
left += 1
elif total > 0:
# Step 7: If the sum is too large, move right pointer left to decrease the sum
right -= 1
else:
# Step 8: Found a valid triplet
result.append([nums[i], nums[left], nums[right]])
# Step 9: Skip duplicate values for left pointer
while left < right and nums[left] == nums[left + 1]:
left += 1
# Step 10: Skip duplicate values for right pointer
while left < right and nums[right] == nums[right - 1]:
right -= 1
# Step 11: Move both pointers inward after finding a valid triplet
left += 1
right -= 1
# Step 12: Return the list of unique triplets
return result
# Example Usage
nums = [-1, 0, 1, 2, -1, -4]
result = threeSum(nums)
print(result)
# Output: [[-1, -1, 2], [-1, 0, 1]]
# Reference Hints & Solutions
# https://neetcode.io/solutions/3sum