↓Skip to main content

Two-Pointers

LeetCode 763: Partition Labels
·2 mins
LeetCode Medium Hash-Table Two-Pointers String Greedy
LeetCode 763 note: record each letter’s last index, extend the current part’s end while scanning, and cut when the scan reaches it
Linked List
·6 mins
Algorithm Linked-List Two-Pointers
Dummy heads, reversal (whole list, a range, half), fast and slow pointers for middles and cycles, two pointers n apart, pairwise swaps, two lists meeting, and copying a list with random pointers via a hash map
Two Pointers
·9 mins
Algorithm Two-Pointers String Sorting
Converging pointers, one pointer per sequence, skipping blanks to compare positions, counting pairs after sorting, and the in-place write pointer
Binary Search
·7 mins
Algorithm Binary-Search Prefix-Sum Two-Pointers
Lower / upper bound, binary search on the answer, k-th smallest, integer roots, and why the two interval styles must not be mixed
LeetCode 493: Reverse Pairs
·4 mins
LeetCode Hard Merge-Sort Divide-and-Conquer Two-Pointers Counting
LeetCode 493 note: when the counting condition differs from the sort order, scan first, then merge
LeetCode 2426: Number of Pairs Satisfying Inequality
·3 mins
LeetCode Hard Merge-Sort Divide-and-Conquer Two-Pointers Counting
LeetCode 2426 note: simplify the inequality, then decide whether the matches form a prefix or a suffix
LeetCode 88: Merge Sorted Array
·3 mins
LeetCode Easy Two-Pointers Merge-Sort
LeetCode 88 note: merge from the back with O(1) extra space
LeetCode 21: Merge Two Sorted Lists
·3 mins
LeetCode Easy Linked-List Two-Pointers Merge-Sort
LeetCode 21 note: the dummy node, and why Python assignment is not a copy
LeetCode 1842: Next Palindrome Using Same Digits
·3 mins
LeetCode Hard Two-Pointers String
LeetCode 1842 note: a palindrome is determined by its left half and the middle digit stays put; take the next permutation of the left half and mirror it
LeetCode 719: Find K-th Smallest Pair Distance
·2 mins
LeetCode Hard Binary-Search Two-Pointers Sliding-Window Sorting
LeetCode 719 note: binary search on the distance, counting pairs with distance <= mid using a sliding window
LeetCode 2193: Minimum Number of Moves to Make Palindrome
·3 mins
LeetCode Hard Two-Pointers Greedy String Binary-Indexed-Tree
LeetCode 2193 note: greedily fix the outer pair: take the last character and swap its leftmost partner to the front, costing that index; the unpaired character ends in the middle, costing its distance there
LeetCode 4026: Maximum Gap Between Stations
·2 mins
LeetCode Medium Two-Pointers Greedy String
LeetCode 4026 note: greedily match from the left for each worker’s earliest station and from the right for the latest; splitting between workers i and i+1, the largest gap is latest[i+1] - earliest[i]