509. Fibonacci Number
Link: https://leetcode.com/problems/fibonacci-number/
Description
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,
F(0) = 0, F(1) = 1
F(n) = F(n - 1) + F(n - 2), for n > 1.
Given n, calculate F(n).
Approach
Dynamic Programming
- If
nequals to0or1, returnn. - Else, initialize a dp array to save the Fibonacci number of the current index. Initialize the the index 0 of the dp array to
0and index 1 to1. - Loop the array from index
2:- Add the sum of the previous index and the index before the pervious index to the current element.
Recursion
- If
nequals to0or1, returnn. - Else, return the sum of the previous index and the index before the pervious index to the current element.
Solution
// DP
class Solution {
public int fib(int n) {
if (n == 0 || n == 1)
return n;
int[] dp = new int[n + 1];
dp[0] = 0;
dp[1] = 1;
for (int i = 2; i <= n; i++)
dp[i] = dp[i - 1] + dp[i - 2];
return dp[n];
}
}
// Recursion
class Solution {
public int fib(int n) {
if (n == 0 || n == 1)
return n;
return fib(n - 1) + fib(n - 2);
}
}
70. Climbing Stairs
Link: https://leetcode.com/problems/climbing-stairs/
Description
You are climbing a staircase. It takes n steps to reach the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Approach
- The dp array is used to store the distinct ways to climb to the top.
- The first step has only one way and the second step has two ways so we initialize index 1 the dp array to 0 and 1 to 2.
- Loop the dp array from index
2:- There are two ways to dlimb to the current index, from the previous step or the step before the previous step. The distinct ways to climb to the current index is the combination of the two ways.
Solution
class Solution {
public int climbStairs(int n) {
int[] dp = new int[n + 1];
dp[1] = 1;
dp[2] = 2;
for (int i = 3; i <= n; i++)
dp[i] = dp[i - 1] + dp[i - 2];
return dp[n];
}
}
746. Min Cost Climbing Stairs
Link: https://leetcode.com/problems/min-cost-climbing-stairs/
Description
You are given an integer array cost where cost[i] is the cost of ith step on a staircase. Once you pay the cost, you can either climb one or two steps.
You can either start from the step with index 0, or the step with index 1.
Return the minimum cost to reach the top of the floor.
Appraoch
- The meaning of the dp array is to store the cost to reach
ithstep. So we need to calculate the cost of the step after the final step. - The first step does not need cost so we initialize index
0and1of the dp array to0. - Loop through the dp array from index
2:- There are two ways to climb to the current index, the first one is climbing from the previous index, the second one is climbing from the index before the previous index. Compute the cost of the two ways and save the smaller one to the current index.
Solution
class Solution {
public int minCostClimbingStairs(int[] cost) {
int[] dp = new int[cost.length + 1];
dp[0] = 0;
dp[1] = 0;
int cnt = 0;
for (int i = 2; i < dp.length; i++) {
dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
}
return dp[cost.length];
}
}
本文介绍了如何使用动态编程和递归方法解决LeetCode上的两个阶梯问题:计算Fibonacci数列和爬楼梯的不同方式,以及最小成本爬楼梯。详细展示了代码实现和思路分析。
204

被折叠的 条评论
为什么被折叠?



