Backward [mp]
Micro pattern¶
Description¶
Traverse from the end when dependencies flow toward earlier positions or forward writes would overwrite unread information.
Problems¶
Avoid overwriting unread values¶
Write results from the back when the destination overlaps an input.
- 88. Merge Sorted Array (Top Easy)
Process least-significant positions first¶
Start at the rightmost digit when carry moves toward more significant positions.
- 66. Plus One (Top Easy)
- 445. Add Two Numbers II (Google Prep)
Accumulate suffix state¶
Traverse right to left while maintaining information about later positions.
- 238. Product of Array Except Self (Top Hard)
- 315. Count of Smaller Numbers After Self (Top Hard)
- 120. Triangle (Dynamic Programming)
- 516. Longest Palindromic Subsequence (Dynamic Programming)
- 1312. Minimum Insertion Steps to Make a String Palindrome (Dynamic Programming)
- 2140. Solving Questions With Brainpower (Dynamic Programming)
Preserve stable ordering¶
Traverse records from right to left when cumulative counts determine their output positions.
- Counting sort (DSA)
- LSD radix sort (DSA)
Descending buckets or reversed partitions¶
Consume groups from high to low when larger values must be emitted first.
- 347. Top K Frequent Elements (Top Medium)
- 324. Wiggle Sort II (Top Hard)