Leetcode刷题Day38-------------------动态规划
1. 理论基础
文章链接:https://programmercarl.com/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html 视频链接:https://www.bilibili.com/video/BV13Q4y197Wg 题目链接:https://leetcode.cn/problems/fibonacci-number/ 动规题目: ------------------------------------- 动规五部曲: (该图截自B站代码随想录视频)
2. 509. 斐波那契数
题目链接:https://leetcode.cn/problems/climbing-stairs/ 文章链接:https://programmercarl.com/0509.%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0.html 视频链接:https://www.bilibili.com/video/BV1f5411K7mo
class Solution {
public int fib ( int n) {
int [ ] ints= new int [ n+ 2 ] ;
ints[ 0 ] = 0 ;
ints[ 1 ] = 1 ;
for ( int i= 2 ; i<= n; i++ ) ints[ i] = ints[ i- 1 ] + ints[ i- 2 ] ;
return ints[ n] ;
}
}
3. 70. 爬楼梯
文章链接:https://programmercarl.com/0070.%E7%88%AC%E6%A5%BC%E6%A2%AF.html 视频链接:https://www.bilibili.com/video/BV17h411h7UH
class Solution {
public int climbStairs ( int n) {
int [ ] ints= new int [ n+ 2 ] ;
ints[ 1 ] = 1 ;
ints[ 2 ] = 2 ;
for ( int i= 3 ; i<= n; i++ ) ints[ i] = ints[ i- 1 ] + ints[ i- 2 ] ;
return ints[ n] ;
}
}
4. 746. 使用最小花费爬楼梯
题目链接:https://leetcode.cn/problems/min-cost-climbing-stairs/ 文章链接:https://programmercarl.com/0746.%E4%BD%BF%E7%94%A8%E6%9C%80%E5%B0%8F%E8%8A%B1%E8%B4%B9%E7%88%AC%E6%A5%BC%E6%A2%AF.html 视频链接:https://www.bilibili.com/video/BV16G411c7yZ
class Solution {
public int minCostClimbingStairs ( int [ ] cost) {
int len = cost. length;
int [ ] dp = new int [ len + 1 ] ;
dp[ 0 ] = 0 ;
dp[ 1 ] = 0 ;
for ( int i = 2 ; i <= len; i++ ) {
dp[ i] = Math . min ( dp[ i - 1 ] + cost[ i - 1 ] , dp[ i - 2 ] + cost[ i - 2 ] ) ;
}
return dp[ len] ;
}
}