2 Pointers
柱状图装水
11. 盛水最多的容器
|
|
42. 接雨水
- Given several plunks standing on an X-axis, caculate the maximum volume of water it can trap.
- O(n) time by mono-stack.
- O(n) time by DP. The amount of water loadable at each point is determined by leftMax and rightMax.
- O(1) space by two pointers from two ends. leftMax and rigthMax is going-to-center mono, thus volume is mono.
|
|
排序
31. 下一个排列
|
|
88. 合并两个有序数组
581. 最短无序连续子数组
|
|
Two pointers sometimes is an implementation of mono-stack or mono-queue. Mono here means that, under the problem’s condition, the optimal value of left pointer is a mono function of right pointer, and vice versa. One needs to find out the mover changing condition, this is typically when moving more won’t make things better.
题解
- 3 Longest Substring Without Repeating Characters
- O(n) time and O(1) space. Mono function is increasing. When there is repeat, moving is bad.
- 15 3Sum
- O(n^2) time. Sort then use two pointers.
- 16 3Sum Closest
- 3Sum but return the triplet whose sum is closest to a target.
- O(n^2) time. Sort and two pointers.
- 26 Remove Duplicates from Sorted Array
- 27 Remove Element
- 30 Substring with Concatenation of All Words
- Given a string and a word (all same length) list, find all substrings that are made up of the whole word list.
- O(nm) time and O(m) space, where n is length of string and m is length of word. Word-level two pointer, start from 0 - m-1.
- DP is also possible. For each index, save the longest valid substring ended with it, and corresponding word usages.
- 75 Sort Colors
- Sort sequences consisting of 1, 2, 3.
- O(n) time and one pass by two pointers. Not related to mono things.
- 76 Minimum Window Substring
- Given S and T, find minimum window in S containing AT LEAST all letters in T.
- O(n) time by two pointers.
- 80 Remove Duplicates from Sorted Array II
- Given a sorted array, remove redundant elements such that no element appears more than 2 times.
- O(n) time by two pointers. Not related to mono things.
- 159 Longest Substring with At Most Two Distinct Characters
- Easy O(n) time two pointers.
- 209 Minimum Size Subarray Sum
- O(n) time by two pointers.
- O(nlog(n)) time by binary search the minimum size.
- 259 3Sum Smaller
- O(n^2) time by sorting first. Similar to 3Sum Closest.
- 287 Find the Duplicate Number
- O(n) time and O(1) space by Floyd Cycle Detection Algorithm. No mono things.
- 340 Longest Substring with At Most K Distinct Characters
- O(n) time and O(1) space, typical sliding window.
- 424 Longest Repeating Character Replacement
- Given a string, one can at most alter k characters. Find the maximum length of repeat character string.
- O(n) time. Note that these k characters must be in a sliding window.
- Use per-character queue also works.
- O(26) space, maintaining maximum character count ever encountered in the sliding window. The length of sliding window is mono increasing.
- 457 Circular Array Loop
- Find circles in integer array. Floyd Cycle Detection Algorithm.
- Pitfall: only mark elements as zero when the case is confirmed invalid.
- 481 Magical String
- Build recursive string: 1221121… -> 1 22 11 2 1 -> 12211…
- Generate string according to its prefix.
- 611 Valid Triangle Number
- Sort and use two pointers to find solve “two sum > nums[largestIdx]”
- 713 Subarray Product Less Than K
- 828 Unique Letter String
- Define UNIQ(s) is the number of unique letters in a string. Find sum of UNIQ of all substrings of S.
- At each step, calculate UNIQ of substrings ending by that character. Maintain a character index map.
- 904 Fruit Into Baskets
- Find longest subarray with at most 2 distinct numbers.
- 986 Interval List Intersections
- Given two non-intersect interval lists, calculate their intersected result.
- Merge sort. Always throw the interval with lower end and keep the rest interval.
- 992 Subarrays with K Different Integers
- Prefix-sum-like method, Find number of subarrays with AT MOST K different integers.
- 1004 Max Consecutive Ones III
- Given a 0-1 array, one can change 0 to 1 at most K times. Find maximum consecutive ones.
- 1031 Maximum Sum of Two Non-Overlapping Subarrays
- Given a number array, find maximum of two non-overlapping M-length and L-length subarray.
- Maintain last M and L length sum and maximum sum of M and L length arrays.
参考资料
Linked Mentions
-
No backlinks found.