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[0]; int c2 = cost[1]; 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

Link: https://leetcode-cn.com/problems/min-cost-clipping-stairs/solution/c-dong-tai-gui-hua-ti-jie-by-da-li-wang-3/

Source: LeetCode

The copyright belongs to the author. For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source.

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

Link: https://leetcode-cn.com/problems/min-cost-clipping-stairs/solution/xiang-dang-yu-yi-ge-fei-bo-na-qi-shu-lie-by-yzwz/

Source: LeetCode

The copyright belongs to the author. For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source.

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[1]=3; if the maximum value of room 2 is 44, it is dp[2]=4; if the value of room 3 itself is 22, it is num=2, then DP [3] = max (DP [2], DP [1] + 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] = 0; dp[1] = nums[0]; 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

Link: https://leetcode-cn.com/problems/house-robbier/solution/hua-jie-suan-fa-198-da-jia-jie-she-by-guanpengchn/

Source: LeetCode

The copyright belongs to the author. For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source.

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[0]; //nums[1] = max(nums[0], nums[1]); 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[1] = 0 #If Alice draws 1, she loses if N<=1: return False else: target[2] = 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

Link: https://leetcode-cn.com/problems/division-game/solution/python3gui-na-fa-by-pandawakaka/

Source: LeetCode

The copyright belongs to the author. For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source.

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; } };