The Count the Number of Incremovable Subarrays II interview question is the optimized version of the previous problem. Given a large array, you must find how many subarrays can be removed to leave a strictly increasing sequence. The challenge here is the constraints: is too slow, so you need an approach.
This "Hard" problem is popular at DE Shaw and Apple. It tests the two pointers interview pattern and the ability to identify prefix/suffix properties. It requires the insight that if you remove a middle section, the remaining part is a prefix concatenated with a suffix. Both the prefix and suffix must themselves be strictly increasing, and the last element of the prefix must be smaller than the first element of the suffix.
The problem uses Two Pointers and Prefix/Suffix Analysis.
nums[p] < nums[s]. Every suffix starting from up to (including removing the whole suffix) works.nums = [1, 2, 3, 1, 5, 6]
[1, 2, 3] (indices 0-2).[1, 5, 6] (indices 3-5).[1, 2] (idx 0-1):
s such that nums[s] > 2.5 (idx 4) or 6 (idx 5) or no suffix.When a problem involves removing a contiguous segment, focus on what remains: a prefix and a suffix. Many Hard-level array problems can be solved by precomputing properties of all possible prefixes and suffixes and then matching them up.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Two Sum II - Input Array Is Sorted | Medium | Solve | |
| Maximum Distance Between a Pair of Values | Medium | Solve | |
| Find K-th Smallest Pair Distance | Hard | Solve | |
| Minimum Time to Eat All Grains | Hard | Solve | |
| Heaters | Medium | Solve |