Skip to content

Commit 9e8fd5f

Browse files
committed
[week3] improvement for 70. Climbing Stairs
1 parent d2358f7 commit 9e8fd5f

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

climbing-stairs/bky373.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
class Solution {
22

33
public int climbStairs(int n) {
4-
int[] ans = new int[n + 1];
5-
if (n <= 2) {
4+
if (n == 1) {
65
return n;
76
}
8-
ans[1] = 1;
9-
ans[2] = 2;
10-
7+
int a1 = 1;
8+
int a2 = 2;
119
for (int i = 3; i < n + 1; i++) {
12-
ans[i] = ans[i - 1] + ans[i - 2];
10+
int a3 = a1 + a2;
11+
a1 = a2;
12+
a2 = a3;
1313
}
14-
return ans[n];
14+
return a2;
1515
}
1616
}
1717
/**
1818
* TC: O(N)
19-
* SC: O(N)
19+
* SC: O(1)
2020
*/

0 commit comments

Comments
 (0)