{"title":"C# 解決方案:爬樓梯的最小花費 (最小成本爬樓梯問題)","description":"C# 爬樓梯的最小花費問題解答,提供修正錯誤的程式碼,解決 List 初始化錯誤,並使用 Math.Min() 方法找到最小成本。","keywords":"C#,爬樓梯,最小花費,演算法,動態規劃,List 初始化,Math.Min(),修正錯誤","content":"class Solution {\n int minCostClimbingStairs(List cost){\n \t\t List dp = new List() { 0, 0 };\n\t\t\tfor(int i = 2; i < cost.Count; i++) dp.Add(Math.Min(dp[i-1]+cost[i-1], dp[i-2]+cost[i-2]));\n\t\t\treturn dp[dp.Count-1];\n }\n}//第4行執行錯誤內容:The error in line 4 is due to the fact that the List class in C# does not have a method called "filled". To fix this error, you can initialize the dp list with two elements and then use a for loop to add the remaining elements. Here's the modified code:\n\ncsharp\nclass Solution {\n int minCostClimbingStairs(List<int> cost){\n \t\t List<int> dp = new List<int>() { 0, 0 };\n\t\t\tfor(int i = 2; i < cost.Count; i++) dp.Add(Math.Min(dp[i-1]+cost[i-1], dp[i-2]+cost[i-2]));\n\t\t\treturn dp[dp.Count-1];\n }\n}\n

C# 解決方案:爬樓梯的最小花費 (最小成本爬樓梯問題)