Dynamic programming problems scare me A LOT.
Yeah, it could be quite frustrating, if you haven’t found the key assertions.
Dynamic Programming is an algorithmic paradigm that solves a given complex problem by breaking it into subproblems and stores the results of subproblems to avoid computing the same results again.
The main idea behind DP is to save duplicated caculations.
Trade space for time.
Basic Abstractions
Name | Summary |
---|---|
optimal substructure | |
DP vs recursive+memoization |
Scenarios
Code Skeleton
Questions
Name | Example |
---|---|
DP needs only O(1) space | LintCode: Computer Maintenance |
Some initialization can be skipped | Leetcode: Longest Arithmetic Subsequence of Given Difference |
Some initialization can be skipped | Leetcode: Bomb Enemy |
Instead of left-to-right, do it from right-to-left | Maximum Length of Repeated Subarray |
Key Parts In DP Problems:
- Key observation is crucial. Watch careful for how the states transit?
- Walk through with smaller cases manually. And detect the pattern.
Different Types Of DP Functions:
- Interesting dp funcitons
Domino and Tromino Tiling
dp(i) = dp(i-1)+dp(i-2)+2*(dp(i-3)+dp(i-4)+…+dp(0)) - DP saves intermediate results, not the final ones
Champagne Tower - dp(i) = min(dp(i), dp[i-coin[j]]+1)
Coin Change - Function: f(i, j): Longest Palindromic Subsequence
- Coin Change 2
- Save the base case: Maximum Length of Repeated Subarray
The most impressive problems to me:
See all dynamicprogramming problems: #dynamicprogramming
- Review: Dynamic Programming Problems
- LintCode: Minimum Cycle Section
- LintCode: Frog Jump
- Lintcode: Digital Flip
- LintCode: Computer Maintenance
- LintCode: Card Game
- Leetcode: Word Break
- Leetcode: Video Stitching
- Leetcode: Valid Palindrome III
- Leetcode: Unique Paths II
- Leetcode: Unique Paths
- Leetcode: Unique Binary Search Trees II
- Leetcode: Unique Binary Search Trees
- Leetcode: Triangle
- Leetcode: Toss Strange Coins
- Leetcode: Tallest Billboard
- Leetcode: Student Attendance Record II
- Leetcode: Shortest Path with Alternating Colors
- Leetcode: Shortest Common Supersequence
- Leetcode: Remove Boxes
- Leetcode: Regular Expression Matching
- Leetcode: Race Car
- Leetcode: Partition Equal Subset Sum
- Leetcode: Paint House
- Leetcode: Paint Fence
- Leetcode: Out of Boundary Paths
- Leetcode: Ones and Zeroes
- Leetcode: One Edit Distance
- Leetcode: New 21 Game
- Leetcode: Minimum Swaps To Make Sequences Increasing
- Leetcode: Minimum Path Sum
- Leetcode: Minimum Falling Path Sum
- Leetcode: Minimum Cost Tree From Leaf Values
- Leetcode: Minimum Cost For Tickets
- Leetcode: Minimum ASCII Delete Sum for Two Strings
- Leetcode: Min Cost Climbing Stairs
- Leetcode: Maximum Sum of Two Non-Overlapping Subarrays
- Leetcode: Maximum Length of Repeated Subarray
- Leetcode: Maximum Length of Pair Chain
- Leetcode: Maximal Square
- Leetcode: Longest Turbulent Subarray
- Leetcode: Longest String Chain
- Leetcode: Longest Palindromic Subsequence
- Protected: Leetcode: Longest Line of Consecutive One in Matrix
- Leetcode: Longest Increasing Subsequence
- Leetcode: Longest Common Subsequence
- Leetcode: Longest Arithmetic Subsequence of Given Difference
- Leetcode: Length of Longest Fibonacci Subsequence
- Leetcode: Last Stone Weight II
- Leetcode: Largest Sum of Averages
- Leetcode: Largest Divisible Subset
- Leetcode: Knight Dialer
- Leetcode: Interleaving String
- Leetcode: Integer Break
- Leetcode: House Robber II
- Leetcode: House Robber
- Leetcode: Gray Code
- Leetcode: Flip String to Monotone Increasing
- Leetcode: Find the Derangement of An Array
- Leetcode: Filling Bookcase Shelves
- Leetcode: Fibonacci Number
- Leetcode: Edit Distance
- Leetcode: Dungeon Game
- Leetcode: Domino and Tromino Tiling
- Leetcode: Divisor Game
- Leetcode: Distinct Subsequences II
- Leetcode: Delete Operation for Two Strings
- Leetcode: Counting Bits
- Leetcode: Count Vowels Permutation
- Leetcode: Count Primes
- Leetcode: Combination Sum IV
- Leetcode: Climbing Stairs
- Leetcode: Champagne Tower
- Leetcode: Can I Win
- Leetcode: Bomb Enemy
- Leetcode: Bitwise ORs of Subarrays
- Leetcode: Best Time to Buy and Sell Stock with Transaction Fee
- Leetcode: Beautiful Array
- Leetcode: Arithmetic Slices II – Subsequence
- Leetcode: All Possible Full Binary Trees
- Leetcode: Airplane Seat Assignment Probability
- Leetcode: 4 Keys Keyboard
- Leetcode: 2 Keys Keyboard
See more blog_posts.