# Detailed analysis of the entry problem of leetcode dynamic planning

Keywords: Programming

1. Minimum cost of climbing stairs
https://leetcode-cn.com/problems/min-cost-climbing-stairs/

The explanation of the big guy's question is the same as that of the official answer (I added notes)

```class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
int c1 = cost;
int c2 = cost;
int N = cost.size();
for (int i = 2; i < N; ++i) {
int c3 = min(c1, c2) + cost[i];
//Starting from i = 2, the physical strength of this layer plus min (reaching the minimum value of the physical strength expended in the I-2 layer and reaching the minimum value of the physical strength expended in the I-1 layer)
c1 = c2;
//c1 is the minimum value of physical strength to reach the I-1 layer
c2 = c3;
//c2 is the minimum value of physical strength to reach level i
}
return min(c1, c2);
//Compare the value of n-1 and n-2's physical exertion
}
};
```

Author: Da Li Wang
Source: LeetCode

Better understanding of this big guy's problem

```class Solution{
public:
int minCostClimbingStairs(vector<int>& cost) {
int num=cost.size();
for(int i=2;i<num;++i)cost[i]+=min(cost[i-1],cost[i-2]);
return min(cost[num-1],cost[num-2]);
}
};
```

By yzwz
Source: LeetCode

2. Rob the house
https://leetcode-cn.com/problems/house-robber/
Specific analysis to see the explanation of the second Tycoon (soul painter)

thinking
Labels: dynamic planning
Dynamic programming equation: DP [n] = max (DP [n-1], DP [n-2] + Num)
Because it is not allowed to break into adjacent houses, the maximum value of N houses can be stolen at the current position is either the maximum value of n-1 houses, or the maximum value of n-2 houses can be stolen plus the value of current houses. The maximum value is taken between the two
For example, if the maximum value of room 1 is 33, it is dp=3; if the maximum value of room 2 is 44, it is dp=4; if the value of room 3 itself is 22, it is num=2, then DP  = max (DP , DP  + Num) = max (4, 3 + 2) = 5; if the maximum value of room 3 is 55
Time complexity: O(n)O(n), nn is the array length

```class Solution {
public int rob(int[] nums) {
int len = nums.length;
if(len == 0)
return 0;
int[] dp = new int[len + 1];
dp = 0;
dp = nums;
for(int i = 2; i <= len; i++) {
dp[i] = Math.max(dp[i-1], dp[i-2] + nums[i-1]);
}
return dp[len];
}
}
```

By: Guan pengchn
Source: LeetCode

I wrote one in C + +

```class Solution {
public:
int rob(vector<int>& nums) {
int len = nums.size();
if(len == 0)
return 0;
if(len == 1)
return nums;
//nums = max(nums, nums);
for(int i = 2; i < len; i++){
nums[i] = max(nums[i] + nums[i - 2], nums[i - 1]);
}
return max(nums[len - 1], nums[len - 2]);
}
};
```

It's because the commented sentence is not added and can't pass the input: [2,1,1,2] correct output: 4
So let's take a look at the big guy

3. Divisor game
https://leetcode-cn.com/problems/divisor-game/
(without looking at the solution, I only think that Alice can win when N-x is a non three prime number. If N = kx, x is not equal to 1, then N-x = (k-1) x = n ', n' cannot be a prime number.)

The induction method of the first big man in the solution to the question is summed up in his own words (Chinese lesson routine)
1. Alice wins 2 and loses 1;
2. If Alice is an odd number at present, the divisor of the odd number can only be an odd number or 1, so the next one must be an even number;
3. If Alice is an even number at present, the divisor of an even number can be an odd number or an even number, so directly subtract 1, then the next one is an odd number, that is to say, Bob must get an odd number, Alice must get an even number, and it will be 2 if it gets smaller all the time;

So, odd lose, even win

```class Solution:
def divisorGame(self, N: int) -> bool:
target = [0 for i in range(N+1)]
target = 0 #If Alice draws 1, she loses
if N<=1:
return False
else:

target = 1 #If Alice draws two, Alice wins
for i in range(3,N+1):
for j in range(1,i//2): #i haven't learned reptile. It means the root sign (?) from 1 to i. j stands for x
# If j is the remainder of i and target[i-j] is false (0), it means that it is currently true (1)
if i%j==0 and target[i-j]==0: #Bob gets i - j, and target[i - j] = 0 means failure, Bob fails Alice's target[i] = 1
target[i] = 1
break
return target[N]==1
```

By pandawakaka
Source: LeetCode

4. Judgment subsequence
https://leetcode-cn.com/problems/is-subsequence/
This is the first topic written by Bo under dynamic planning, although it is not a dynamic planning method~

```class Solution {
public:
bool isSubsequence(string s, string t) {
int len1 = s.length();
long long len2 = t.length();
long long ma = len2;
for(int i = len1 - 1; i >= 0; i--){
bool f = false;
for(int j = ma - 1; j >= 0; j--){
if(s[i] == t[j]){
ma = j;
f = true;
break;
}
}
if(f == false){
return false;
}
}
return true;
}
};
```  Published 4 original articles, won praise 0, visited 36

Posted by HK2ALL on Sun, 09 Feb 2020 01:07:52 -0800