FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_04/id_1/LeetCode_746_1.java at master · algorithm001/algorithm · GitHub
algorithm001
/
algorithm
Public
Notifications
You must be signed in to change notification settings
Fork
148
Star
118
Code
Issues
548
Pull requests
46
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm
/
Week_04
/
id_1
/
LeetCode_746_1.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
36 lines (25 loc) · 910 Bytes
Breadcrumbs
algorithm
/
Week_04
/
id_1
/
LeetCode_746_1.java
Copy path
File metadata and controls
36 lines (25 loc) · 910 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// 解法正确但是因为大量重复计算超时
// class Solution {
// private int[] cost;
// public int minCostClimbingStairs(int[] cost) {
// this.cost = cost;
// int len = cost.length;
// return Math.min(minCost(len-1), minCost(len-2));
// }
// public int minCost(int n) {
// if (n <= 1) {
// return this.cost[n];
// }
// return this.cost[n] + Math.min(minCost(n-1), minCost(n-2));
// }
// }
// 参考 discuss 部分的结果,将数据中的值相加,而不是取值计算
class
Solution
{
private
int
[]
cost
;
public
int
minCostClimbingStairs
(
int
[]
cost
) {
for
(
int
i
=
2
;
i
<
cost
.
length
;
i
++) {
cost
[
i
] +=
Math
.
min
(
cost
[
i
-
1
],
cost
[
i
-
2
]);
}
return
Math
.
min
(
cost
[
cost
.
length
-
1
],
cost
[
cost
.
length
-
2
]);
}
}
Back
|
FazBrowse Home
|
New Git URL