| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,33 @@ | |||
| 1 | + | ||
| 2 | + // 解法正确但是因为大量重复计算超时 | ||
| 3 | + // class Solution { | ||
| 4 | + // private int[] cost; | ||
| 5 | + // public int minCostClimbingStairs(int[] cost) { | ||
| 6 | + | ||
| 7 | + // this.cost = cost; | ||
| 8 | + // int len = cost.length; | ||
| 9 | + | ||
| 10 | + // return Math.min(minCost(len-1), minCost(len-2)); | ||
| 11 | + // } | ||
| 12 | + | ||
| 13 | + // public int minCost(int n) { | ||
| 14 | + | ||
| 15 | + // if (n <= 1) { | ||
| 16 | + // return this.cost[n]; | ||
| 17 | + // } | ||
| 18 | + // return this.cost[n] + Math.min(minCost(n-1), minCost(n-2)); | ||
| 19 | + | ||
| 20 | + // } | ||
| 21 | + // } | ||
| 22 | + | ||
| 23 | + // 参考 discuss 部分的结果,将数据中的值相加,而不是取值计算 | ||
| 24 | + class Solution { | ||
| 25 | + private int[] cost; | ||
| 26 | + public int minCostClimbingStairs(int[] cost) { | ||
| 27 | + | ||
| 28 | + for (int i = 2; i < cost.length; i++) { | ||
| 29 | + cost[i] += Math.min(cost[i-1], cost[i-2]); | ||
| 30 | + } | ||
| 31 | + return Math.min(cost[cost.length-1], cost[cost.length-2]); | ||
| 32 | + } | ||
| 33 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments