Open JinJinQuant opened 4 days ago
Difficulty | Category | Problem | Number |
---|---|---|---|
Easy | Two Pointers | Merge Sorted Array | 1 |
Easy | Two Pointers | Remove Duplicates from Sorted Array | 1 |
Medium | Two Pointers | 3Sum | 1 |
Medium | Two Pointers | Longest Palindromic Substring | 1 |
Easy | Two Pointers | Move Zeroes | 1 |
Medium | Two Pointers | Container With Most Water | 1 |
Hard | Two Pointers | Trapping Rain Water | 1 |
Medium | Two Pointers | Interval List Intersections | 2 |
Easy | Two Pointers | Count Pairs Whose Sum is Less than Target | 2 |
Medium | Two Pointers | Number of Substrings Containing All Three Characters | 2 |
Easy | Two Pointers | Sort Array By Parity | 3 |
Easy | Two Pointers | Squares of a Sorted Array | 3 |
Medium | Two Pointers | Remove Duplicates from Sorted Array II | 3 |
Easy | Dynamic Programming | Best Time to Buy and Sell Stock | 1 |
Easy | Dynamic Programming | Fibonacci Number | 1 |
Medium | Dynamic Programming | Maximum Subarray | 1 |
Medium | Dynamic Programming | Best Time to Buy and Sell Stock II | 1 |
Easy | Dynamic Programming | Min Cost Climbing Stairs | 1 |
Medium | Dynamic Programming | Maximum Subarray Sum with One Deletion | 2 |
Hard | Dynamic Programming | Best Time to Buy and Sell Stock III | 2 |
Medium | Dynamic Programming | Coin Change | 2 |
Medium | Dynamic Programming | Jump Game | 2 |
Medium | Dynamic Programming | House Robber | 2 |
Medium | Dynamic Programming | Longest Common Subsequence | 2 |
Medium | Dynamic Programming | Unique Paths | 2 |
Medium | Dynamic Programming | Maximum Product Subarray | 2 |
Medium | Dynamic Programming | Longest Arithmetic Subsequence of Given Difference | 2 |
Medium | Dynamic Programming | Coin Change II | 2 |
Medium | Dynamic Programming | Jump Game II | 2 |
Medium | Dynamic Programming | Partition Equal Subset Sum | 2 |
Medium | Dynamic Programming | House Robber II | 2 |
Medium | Dynamic Programming | Minimum Path Sum | 2 |
Medium | Dynamic Programming | Integer Break | 2 |
Medium | Dynamic Programming | Maximum Profit From Trading Stocks | 2 |
Hard | Dynamic Programming | Super Egg Drop | 3 |
Medium | Dynamic Programming | Best Time to Buy and Sell Stock with Transaction Fee | 3 |
Use two pointers to find the target value by pointing to different elements in a one-dimensional array
A brute-force solution with O(n^2)
time-complexity can always be optimized to O(n)
performance
Try the two pointers approach when you want to process elements in a continuous interval or figure out something in a sorted array
Two pointers move in the same direction
Two pointers move in opposite directions from both ends
One pointer moves in one direction only while the other pointer moves in both directions
Start from the end
class Solution:
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
"""
Do not return anything, modify nums1 in-place instead.
"""
p1 = m-1
p2 = n-1
for p in range(n+m-1, -1, -1):
if p2 < 0:
break
if p1 >= 0 and nums1[p1] > nums2[p2]:
nums1[p] = nums1[p1]
p1 -= 1
else:
nums1[p] = nums2[p2]
p2 -= 1
Start from the beginning
26. Remove Duplicates from Sorted Array
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
j = 1
for i in range(1, len(nums)):
if nums[i] != nums[i-1]:
nums[j] = nums[i]
j += 1
return j
It is necessary to review Two Sum
Two pointers (O(n)
)
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
ans = []
nums.sort()
for i in range(len(nums)):
if nums[i] > 0:
break
if i == 0 or nums[i-1] != nums[i]:
self.twoSum(nums, i, ans)
return ans
def twoSum(self, nums, i, ans):
pl, ph = i+1, len(nums)-1
while pl < ph:
s = nums[i] + nums[pl] + nums[ph]
if s < 0: # -nums[i] > nums[p1] + nums[ph] -> need to increase
pl += 1
elif s > 0:
ph -= 1
else:
ans.append([nums[i], nums[pl], nums[ph]])
pl += 1
ph -= 1
while pl < ph and nums[pl] == nums[pl-1]:
pl += 1