List
题号 | 题目链接 | 讲解链接 | 说明 |
---|---|---|---|
一维 | |||
70 | Climbing Stairs | 视频讲解 | Done |
62 | Unique Paths | 视频讲解 | Done |
63 | Unique Paths II | 视频讲解 | Done |
120 | Triangle | 视频讲解 | 很少考 |
279 | Perfect Squares | 视频讲解 | Done(Essentially an mathmatic problem) |
139 | Word Break | 视频讲解 | Done |
375 | Guess Number Higher or Lower II | 视频讲解 | hard to understand |
312 | Burst Balloons | 视频讲解 | |
322 | Coin Change | 视频讲解 | |
二维 | |||
256 | Paint House | 视频讲解 | Done |
265 | Paint House II | 视频讲解 | Done(half) |
64 | Minimum Path Sum | 视频讲解 | Done |
72 | Edit Distance | 视频讲解 | |
97 | Interleaving String | 视频讲解 | |
174 | Dungeon Game | 视频讲解 | |
221 | Maximal Square | 视频讲解 | |
85 | Maximal Rectangle | 视频讲解 | |
363 | Max Sum of Rectangle No Larger Than K | 视频讲解 | TreeSet |
化简 | |||
198 | House Robber | 视频讲解 | |
213 | House Robber II | 视频讲解 | |
276 | Paint Fence | 视频讲解 | |
91 | Decode Ways | 视频讲解 | |
10 | Regular Expression Matching | 视频讲解 | |
44 | Wildcard Matching | 视频讲解 |
279
Description
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...
) which sum to n.
Example 1:
1 | Input: n = 12 |
Example 2:
1 | Input: n = 13 |
explanation and my thought
Well, it is straightforward to come up with the dp formula:
In other words, we just define the same problem with an smaller input m as the subproblem, and calculate the optimal solution by reuse the result of subproblem. And it would take $O(N^2)$ to terminate since in each subproblem we need to traverse $O(N)$ subproblem to get the answer and there is totally $N$ subproblem.
The fastest solution in the discussion board is basically a mathematic one. It is pretty but could not be called as very useful one when you are in an interview. Just think about how embarrassing it would be when you gave that genius solution and your interviewer ask you about some details of this algorithm. What you want to say? Show them the paper and proof? LOL.
Code
1 | map<int,int>dp; |
139 Word Break
Description
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
Note:
- The same word in the dictionary may be reused multiple times in the segmentation.
- You may assume the dictionary does not contain duplicate words.
Example 1:
1 | Input: s = "leetcode", wordDict = ["leet", "code"] |
Example 2:
1 | Input: s = "applepenapple", wordDict = ["apple", "pen"] |
Example 3:
1 | Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"] |
Explanation and my thought
First Let’s talk about how could you find that it is a dynamic programming problem:
Firstly, greedy is not suitable there since there could be multiple choice to split the string and the result varies by each split:
in the example:
s
=leetcode
, wordDict
=["lee","leet",""code"]
, if you do things greedily, you might choose the “lee” as the first word to split the s
then you get stuck there since there is no word like tcode
in the wordDict
.
So the dynamic programming method here, and firstly let’s define the subproblem, there are two choices here, the first is to define the subproblem dp[i] so that the dp[i] is the answer for a substring start at index 0 and ends at index i, and the second solution is to define the subproblem as dp[i] [j] for the subproblem with substring start at i and ends at j.
If we observe the example, we could find that every valid string could be split like dp[j]+aWordInWordDict(j is some index >0 and <n). It could be proved using contradiction, so an one dimention array is enough to solve this problem, and the dp recurrence formula is like that:
Code
1 | class Solution { |