~/problems
Problems
Basics first, then the classics, then company-style assessments. Every problem has tests you run right here; multi-level ones unlock as you go. See the roadmap.
Longest increasing subsequence
O(n log n) with tails[] + bisect; rebuild via parent pointers.
Notes
Recognise it when: you need the longest increasing chain, nesting (envelopes, after sorting), or the minimum number of decreasing sequences.
tails = [] # tails[k] = smallest tail of an increasing subsequence of length k+1
for x in a:
i = bisect.bisect_left(tails, x) # bisect_right for non-decreasing
if i == len(tails): tails.append(x)
else: tails[i] = x
return len(tails)
O(n log n). To rebuild the sequence, store the index of the tail at each length plus a parent array.
Russian dolls: sort by (width ascending, height descending), then take the LIS on heights.
6 problems
Interview roadmap
1-D dynamic programming One index of state: stairs, robbers, subsequences.
Longest increasing subsequence guide
- Basics: LIS length ending at each index (O(n²) DP) basics py · c++ · java easy
- Choir line with a height gap py · c++ · java medium
- Longest Increasing Subsequence py · c++ · java medium
- Russian Doll Envelopes py · c++ · java hard
- Towers py · c++ · java medium
- Reconstruct a longest increasing subsequence py · c++ · java medium