Introduction to leetcode 21 day dynamic planning -- climbing stairs from 0 to 0.5 [Day02]

Posted by R0d Longfella on Sat, 25 Dec 2021 17:03:28 +0100

Introduction to leetcode 21 day dynamic planning -- climbing stairs from 0 to 0.5 [Day02]

Write in front

Then in the last article, today Xiaofu ran up and down around the school built around the mountain, in order to eat shrimp dumplings with good taste. After all, today is the winter solstice. I hope all friends can also eat delicious dumplings. I don't say much. Then we came yesterday. The next day, we climbed the stairs

DAY2

Topic 1

  1. Stair climbing difficulty coefficient:*
Suppose you are climbing stairs. need n You can't reach the roof until you step down.
You can climb one or two steps at a time. How many different ways can you climb to the roof?
Note: given n Is a positive integer.

Example

Example 1:

Input: 2
 Output: 2
 Explanation: there are two ways to climb to the roof.
1.  1 rank + 1 rank
2.  2 rank

Example 2:

Input: 3
 Output: 3
 Explanation: there are three ways to climb to the roof.
1.  1 rank + 1 rank + 1 rank
2.  1 rank + 2 rank
3.  2 rank + 1 rank

Tips

1<= n <=45

thinking

Then, according to the idea of dynamic programming, infer the whole idea with a small part.
Let's observe first
1,When n = 1 There is a way to get to the roof
	1.	1 rank
2,When n = 2 There are two ways to reach the roof
	1.  1 rank + 1 rank
	2.  2 rank
3,When n = 3 There are three ways to reach the roof
	1.  1 rank + 1 rank + 1 rank
	2.  1 rank + 2 rank
	3.  2 rank + 1 rank
4,When n = 4 There are five ways to get to the roof
	1.  1 rank + 1 rank + 1 rank + 1 rank
	2.  1 rank + 2 rank + 1 rank
	3.  2 rank + 1 rank + 1 rank
	4.  1 rank + 1 rank + 2 rank
	5.  2 rank + 2 rank

Do you find any rules now?
Because the way to climb the n th stair is equal to

  • The way to climb the nth - 1st stair is to climb another stair, that is + 1
  • The way to climb the nth - 2nd stair is to climb up two stairs, that is + 2

So we can get the relation
Fn = Fn-1 + Fn-2
I don't need to teach the rest. Just like yesterday's dynamic programming, a small part of the simulation is launched.

code implementation

class Solution {
    public int climbStairs(int n) {
        if (n < 3)return n;
        int n1 = 1;
        int n2 = 2;
        int n3 = n1 + n2;
        //Simulation dynamic derivation
        for (int i = 3;i < n;i++){
            n1 = n2 ;
            n2 = n3 ;
            n3 = n1 + n2;
        }
        return n3;
    }
}

results of enforcement

Topic 2

  1. Difficulty factor of climbing stairs with minimum cost:*
Each subscript of the array acts as a ladder,
The first i Steps correspond to a non negative physical cost value cost[i](Subscripts start at 0).
Every time you climb a ladder, you have to spend the corresponding physical strength,
Once you have paid the corresponding physical strength, you can choose to climb up one step or two steps.
Please find out the lowest cost to reach the top of the floor.
At the beginning, you can select the element with subscript 0 or 1 as the initial ladder.

Example

Example 1:

Input: cost = [10, 15, 20]
Output: 15
 Explanation: the minimum cost is from cost[1] Start, and then take two steps to the top of the ladder. It costs a total of 15.

Example 2:

Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
Output: 6
 Explanation: the lowest cost is from cost[0] Start, go through those 1 one by one,
skip cost[3] ,The total cost is 6.

Tips

The length range of cost is [2, 1000].
cost[i] will be an integer data with a range of [0, 999].

thinking

Here, Xiaofu gives a brief overview of the questions and extracts keywords
For many small partners who said they didn't understand the problem, Xiao Fu came to make an abstract concept for everyone

  1. Each step has a certain number of obstacles, and only one or two steps can be crossed at a time,
    Go to a ladder to clear the obstacles above. Ask how to get to the top with the least obstacles?
    At the beginning, you choose one of the first two steps as the starting point and clear the obstacles of the current step.
    Of course, there is a barrier free ladder in the last ladder, reaching the peak.

  2. Well, now it's the problem of dynamic programming again. Plug, let's use a small part to simulate and deduce the overall situation.

1. First, we need to maintain an array to record the minimum physical effort required to reach this ladder
2. Conduct simulation to find the formula of dynamic programming:

The minimum fee for reaching the current floor n is equal to:

  • The sum of the physical strength required to reach the N - 1st floor + the physical strength required to climb the N - 1st floor
  • The sum of the physical strength required to reach the N - 2nd floor + the physical strength required to climb the N - 2nd floor
  • Take the minimum value
sumCost[i] = Math.min(sumCost[i-1]+ cost[i-1],sumCost[i-2]+cost[i-2]);

code implementation

class Solution {
    public int minCostClimbingStairs(int[] cost) {
        int sumCost[] = new int [cost.length+1];
        sumCost[0] = sumCost[1] = 0;
        for (int i = 2 ;i< cost.length + 1;i++){
            sumCost[i] = Math.min(sumCost[i-1]+ cost[i-1],sumCost[i-2]+cost[i-2]);
        }
        return sumCost[cost.length];
    }
}

results of enforcement

Write at the end

Today is the second day of introduction to dynamic planning
Stick to clock in
Because now it's just the beginning stage
So don't think that the problem is relatively simple, so you have the idea of "simple problem" for dynamic programming
It's very basic. Let's go step by step
I'm sure you'll have a big receipt in 21 days
last
Make progress every day and harvest every day
May you succeed in your career and learn
If you feel good, don't forget to click three times~

Topics: Java Algorithm leetcode Back-end Dynamic Programming