FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

第四周作业 · algorithm001/algorithm@5b549f6 · GitHub

Commit 5b549f6

Browse files
committed
第四周作业
1 parent 519fba8 commit 5b549f6

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

‎Week_04/id_1/LeetCode_746_1.java‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL