Sub topic 3
Strict vs Non-Decreasing, Applications, and Related Views
This chapter assumes you know the O(n log n) tails-array algorithm, Python's bisect module, and the O(n^2) DP from the earlier chapters.
Strict vs Non-Decreasing: bisect_left vs bisect_right
The tails algorithm changes from one variant to the other through a single choice. bisect_left finds the first slot whose value is >= x, so an equal value replaces that slot and cannot extend the sequence. That gives a strictly increasing result. bisect_right finds the first slot whose value is > x, so an equal value lands after its twin and extends the sequence. That gives a non-decreasing result.
from bisect import bisect_right
def longest_non_decreasing_length(a):
tails = []
for x in a:
i = bisect_right(tails, x)
if i == len(tails):
tails.append(x)
else:
tails[i] = x
return len(tails)
print(longest_non_decreasing_length([1, 3, 3, 2, 3])) # 4Applications: Nesting Envelopes and Minimum Deletions
Minimum deletions. To make a sequence sorted, delete n - L elements, where L is the length of the LIS of the matching variant.
Nesting envelopes. Sort the envelopes by width ascending, and by height descending when widths are equal. Then take the strict LIS of the heights. The descending tie-break stops two envelopes of the same width from nesting.
from bisect import bisect_left
def max_envelopes(envelopes):
envelopes = sorted(envelopes, key=lambda e: (e[0], -e[1]))
tails = []
for _, h in envelopes:
i = bisect_left(tails, h)
if i == len(tails):
tails.append(h)
else:
tails[i] = h
return len(tails)
print(max_envelopes([[5, 4], [6, 4], [6, 7], [2, 3]])) # 3Related Formulations
| View | Idea | Cost |
|---|---|---|
| LCS reduction | The strict LIS of `a` equals the longest common subsequence (LCS) of `a` and `sorted(set(a))` | O(n^2) |
| Counting | Keep `count[i]` next to `length[i]`. When a longer length is found, copy `count[j]`; when an equal length is found, add `count[j]` | O(n^2) |
| Fenwick tree | Compress values to ranks, then `dp = query(rank - 1) + 1` over a prefix-maximum tree | O(n log n) |
| Patience sorting | Place each card on the leftmost pile whose top is `>= x`; the number of piles equals the LIS length | O(n log n) |
The piles explain why the tails method works. Each pile is a non-increasing run, and the pile tops are exactly the tails array. By the dual of Dilworth's theorem (Mirsky's theorem), the minimum number of non-increasing subsequences needed to cover the sequence equals the length of the longest strictly increasing subsequence.
Exercise
How many deletions make [3, 1, 2, 2, 5] strictly increasing, and how many make it non-decreasing?
Show answer
The strict LIS is [1, 2, 5] with length 3, so 2 deletions are needed. The non-decreasing LIS is [1, 2, 2, 5] with length 4, so 1 deletion is needed.
Exercise
In the envelope problem, what goes wrong if equal widths are sorted by ascending height?
Show answer
For example, [6, 4] and [6, 7] would yield heights 4 and 7 in increasing order. The LIS would then count one same-width envelope as nesting inside the other, which overstates the answer.