Two Pointers in One Array
15. 3Sum
Leetcode: https://leetcode.com/problems/3sum/
Note:
- Sort array (to conveniently jump over duplicates)
- Use
i
as the main pointer,l = i + 1
,r = len(nums) - 1
- If current sum == 0: we need to move both
l
andr
.
1 | def threeSum(self, nums): |
16. 3Sum Closest
Leetcode: https://leetcode.com/problems/3sum-closest/
My binary search solution:
1 | def threeSumClosest(self, nums, target): |
Two Pointers solution: (Same as the 3 sum problem)
1 | def threeSumClosest(self, nums: List[int], target: int) -> int: |
259. 3Sum Smaller
Leetcode: https://leetcode.com/problems/3sum-smaller/
Note:
- Doesn’t need to consider the duplicates problem.
- If sum of numbers at
i
,l
,r
is smaller than target, then the sum would also smaller than the target ifr
keeps moving tol
. Thuscnt += r - l
1 | def threeSumSmaller(self, nums: List[int], target: int) -> int: |
18. 4Sum
Leetcode: https://leetcode.com/problems/4sum/
My Backtracking solution(TLE)
1 | def fourSum(self, nums, target): |
Two Pointers solution:
Note:
- Should notice jumping over duplicate numbers
- Calculate 3 sum first then calculate 4 sum
1 | def fourSum(self, nums: List[int], target: int) -> List[List[int]]: |
454. 4Sum II
Leetcode: https://leetcode.com/problems/4sum-ii/
Actually it’s not a two-pointer problem.
Note:
- Count the number of sums of all the combinations in A and B.
- Calculate sums in C and D and see if there’s any match in
counter[-c-d]
1 | def fourSumCount(self, A: List[int], B: List[int], C: List[int], D: List[int]) -> int: |
923. 3Sum With Multiplicity
Leetcode: https://leetcode.com/problems/3sum-with-multiplicity/
Note:
- When
s == target
, we have two situations to consider:
1). The left and right number are the same
2). The left and right number are different - When they are different, we need to count both left and right numbers and multiply them (Note that we need to use
l + 1 < r
rather thanl < r
to count all the numbers) - When they are the same:
1
2
3
4C(n, m) = n! / (m!(n - m)!)
n = X, m = 2
C(X, 2) = X! / (2!(X - 2)!) = X(X - 1)(X - 2)! / (2! / (X - 2)!)
= X (X - 1) / 2
Thus we do cnt += (r - l + 1) * (r - l) / 2
.
1 | def threeSumMulti(self, nums, target): |
611. Valid Triangle Number
Leetcode: https://leetcode.com/problems/valid-triangle-number/
Note:
- When starting searching for the next round, we don’t need to reset
r
toj + 1
, instead, we can directly start from where we stopped in the last round. - Since all the edges are triangle edge, don’t forget about to check if
nums[i] == 0
1 | def triangleNumber(self, nums: List[int]) -> int: |
11. Container With Most Water
Leetcode: https://leetcode.com/problems/container-with-most-water/
- The area is decided by the shortest border, we move whichever border shorter to the center
1 | def maxArea(self, height: List[int]) -> int: |
Two array/list problem
88. Merge Sorted Array
Leetcode: https://leetcode.com/problems/merge-sorted-array/submissions/
Note
Remember to add what’s left in list B into list A.
1 | def merge(self, A: List[int], m: int, B: List[int], n: int) -> None: |