Day 38 | 509. Fibonacci Number | 70. Climbing Stairs | 746. Min Cost Climbing Stairs

该文列举了一系列LeetCode上的动态规划和递归解题实例,包括斐波那契数列、爬楼梯问题和最小成本爬楼梯等,展示了如何利用动态规划数组解决这些计算问题。

Day 1 | 704. Binary Search | 27. Remove Element | 35. Search Insert Position | 34. First and Last Position of Element in Sorted Array
Day 2 | 977. Squares of a Sorted Array | 209. Minimum Size Subarray Sum | 59. Spiral Matrix II
Day 3 | 203. Remove Linked List Elements | 707. Design Linked List | 206. Reverse Linked List
Day 4 | 24. Swap Nodes in Pairs| 19. Remove Nth Node From End of List| 160.Intersection of Two Lists
Day 6 | 242. Valid Anagram | 349. Intersection of Two Arrays | 202. Happy Numbe | 1. Two Sum
Day 7 | 454. 4Sum II | 383. Ransom Note | 15. 3Sum | 18. 4Sum
Day 8 | 344. Reverse String | 541. Reverse String II | 替换空格 | 151.Reverse Words in a String | 左旋转字符串
Day 9 | 28. Find the Index of the First Occurrence in a String | 459. Repeated Substring Pattern
Day 10 | 232. Implement Queue using Stacks | 225. Implement Stack using Queue
Day 11 | 20. Valid Parentheses | 1047. Remove All Adjacent Duplicates In String | 150. Evaluate RPN
Day 13 | 239. Sliding Window Maximum | 347. Top K Frequent Elements
Day 14 | 144.Binary Tree Preorder Traversal | 94.Binary Tree Inorder Traversal| 145.Binary Tree Postorder Traversal
Day 15 | 102. Binary Tree Level Order Traversal | 226. Invert Binary Tree | 101. Symmetric Tree
Day 16 | 104.MaximumDepth of BinaryTree| 111.MinimumDepth of BinaryTree| 222.CountComplete TreeNodes
Day 17 | 110. Balanced Binary Tree | 257. Binary Tree Paths | 404. Sum of Left Leaves
Day 18 | 513. Find Bottom Left Tree Value | 112. Path Sum | 105&106. Construct Binary Tree
Day 20 | 654. Maximum Binary Tree | 617. Merge Two Binary Trees | 700.Search in a Binary Search Tree
Day 21 | 530. Minimum Absolute Difference in BST | 501. Find Mode in Binary Search Tree | 236. Lowes
Day 22 | 235. Lowest Common Ancestor of a BST | 701. Insert into a BST | 450. Delete Node in a BST
Day 23 | 669. Trim a BST | 108. Convert Sorted Array to BST | 538. Convert BST to Greater Tree
Day 24 | 77. Combinations
Day 25 | 216. Combination Sum III | 17. Letter Combinations of a Phone Number
Day 27 | 39. Combination Sum | 40. Combination Sum II | 131. Palindrome Partitioning
Day 28 | 93. Restore IP Addresses | 78. Subsets | 90. Subsets II
Day 29 | 491. Non-decreasing Subsequences | 46. Permutations | 47. Permutations II
Day 30 | 332. Reconstruct Itinerary | 51. N-Queens | 37. Sudoku Solver
Day 31 | 455. Assign Cookies | 376. Wiggle Subsequence | 53. Maximum Subarray
Day 32 | 122. Best Time to Buy and Sell Stock II | 55. Jump Game | 45. Jump Game II
Day 34 | 1005. Maximize Sum Of Array After K Negations | 134. Gas Station | 135. Candy
Day 35 | 860. Lemonade Change | 406. Queue Reconstruction by Height | 452. Minimum Number of Arrows
Day 36 | 435. Non-overlapping Intervals | 763. Partition Labels | 56. Merge Intervals
Day 37 | 738. Monotone Increasing Digits | 714. Best Time to Buy and Sell Stock | 968. BT Camera


LeetCode 509. Fibonacci Number

Question Link

class Solution {
    public int fib(int n) {
        if(n < 2)
            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];
    }
}
  • Determine the meaning of dp[i]
    • The value of ith Fibonacci Number is dp[i]
  • Determine recursive formula
    • dp[i] = dp[i - 1] + dp[i - 2]
  • Initialize the dynamic programming array
    • dp[0] = 0
    • dp[1] = 1
  • The traverse order is front to back, because dp[i] depends on dp[i - 1] and dp[i - 2]
  • Print the array

LeetCode 70. Climbing Stairs

Question Link

class Solution {
    public int climbStairs(int n) {
        if(n < 2)
            return 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];
    }
}
  • Determine the meaning of dp[i]
    • The number of distinct ways to the ith stair is dp[i]
  • Determine recursive formula
    • dp[i] = dp[i - 1] + dp[i - 2]
  • Initialize the dynamic programming array
    • dp[1] = 1
    • dp[2] = 2
  • The traverse order is front to back, because dp[i] depends on dp[i - 1] and dp[i - 2]
  • Print the array

LeetCode 746. Min Cost Climbing Stairs

Question Link

class Solution {
    public int minCostClimbingStairs(int[] cost) {
        int[] dp = new int[cost.length + 1];
        // We can start from index `0` or `1` 
        dp[0] = 0;
        dp[1] = 0;
        for(int i = 2; i <= cost.length; i++)
            dp[i] = Math.min(dp[i-1] + cost[i-1], dp[i-2] + cost[i-2]);
        
        return dp[cost.length];
    }
}
  • Determine the meaning of dp[i]
    • The minimum cost to reach the ith stairs is dp[i]
  • Determine recursive formula
    • dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2])
  • Initialize the dynamic programming array
    • dp[0] = 0
    • dp[1] = 0
  • The traverse order is front to back, because dp[i] depends on dp[i - 1] and dp[i - 2]
  • Print the array
<think>好的,我需要帮助用户查找LeetCode70题的解决方案和题目描述。首先,我应该确认用户的问题是关于LeetCode的爬楼梯问题,即Climbing Stairs。根据用户提供的引用信息,引用[1]、[3]和[5]提到了这个问题,尤其是引用[3]给出了一个具体的例子,输入3时输出3,说明有3种方法爬到顶部。 接下来,我需要整理题目描述。题目通常要求给定n阶楼梯,每次可以爬1或2步,返回不同的方法数。根据引用[3]的例子,当n=3时,答案是3,这符合斐波那契数列的规律,因为f(3)=f(2)+f(1)=2+1=3。 然后,解决方案部分。用户提供的引用[1]中提到了一种优化方法,将时间复杂度从$O(n^2)$降低到$O(n)$,可能是指动态规划的方法。动态规划是解决这个问题的常用方法,因为每一步的方法数等于前一步和前两步的方法数之和。此外,还可以使用递归或记忆化搜索,但动态规划在时间上更高效。 接下来,我需要检查是否有其他方法,比如矩阵快速幂或斐波那契公式,但根据用户提供的引用,可能只需要提到动态规划和递归两种方法。同时,引用[5]中提到的Python链表操作可能不相关,但需要注意Python的自动内存管理,这可能影响代码的写法,比如不需要手动删除节点。 然后,需要确保代码示例正确。动态规划的Python实现应该使用循环,从1到n逐步计算。递归方法可能会有栈溢出的问题,但作为示例可以提及。根据引用[1]的代码示例,用户可能希望看到简洁的代码,如使用变量交换来优化空间复杂度到$O(1)$。 最后,生成相关问题。用户可能对时间复杂度、空间复杂度优化、不同解法比较感兴趣,比如动态规划与递归的区别,或者如何用数学方法解决。同时,应用场景可能涉及斐波那契数列的其他应用。 需要确保回答结构清晰,分点说明题目描述、解决方案和代码示例,并正确引用用户提供的资料。特别是引用[1]和[3]应出现在适当的位置,比如动态规划的解释和示例部分。</think>### LeetCode Problem 70. Climbing Stairs 题解与题目描述 #### 题目描述 给定一个整数$n$表示楼梯阶数,每次可以爬1或2个台阶,求有多少种不同的方法可以到达楼顶[^3]。 示例: 当$n=3$时,共有3种方法: 1. 1步 + 1步 + 1步 2. 1步 + 2步 3. 2步 + 1步 --- #### 解决方案 该问题本质是斐波那契数列的变种,递推公式为: $$f(n) = f(n-1) + f(n-2)$$ 其中$f(1)=1$,$f(2)=2$。 ##### 方法1:动态规划(优化空间) 通过滚动变量优化空间复杂度至$O(1)$,时间复杂度$O(n)$: ```python def climbStairs(n: int) -> int: if n <= 2: return n a, b = 1, 2 for _ in range(3, n+1): a, b = b, a + b return b ``` ##### 方法2:递归(带记忆化) 通过缓存中间结果减少重复计算,时间复杂度$O(n)$: ```python from functools import lru_cache @lru_cache(maxsize=None) def climbStairs(n: int) -> int: if n <= 2: return n return climbStairs(n-1) + climbStairs(n-2) ``` --- #### 复杂度分析 | 方法 | 时间复杂度 | 空间复杂度 | 适用场景 | |------------|------------|------------|------------------------| | 动态规划 | $O(n)$ | $O(1)$ | 大规模输入(推荐) | | 递归 | $O(n)$ | $O(n)$ | 小规模输入或教学示例 | --- 相关问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值