-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https://leetcode.com/problems/climbing-stairs/description/
- Loading branch information
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
interview_prep/algorithm/java/src/main/java/hoa/can/code/ez/ClimbStairs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package hoa.can.code.ez; | ||
|
||
/** | ||
* <a href="https://leetcode.com/problems/climbing-stairs/">desc</a> | ||
*/ | ||
public class ClimbStairs { | ||
public int climbs(int n) { | ||
if (n <= 2) { | ||
return n; | ||
} | ||
|
||
int[] dp = new int[n + 1]; | ||
dp[1] = 1; | ||
dp[2] = 2; | ||
|
||
for (int i = 3; i <= n; i++) { | ||
// either one or two steps away | ||
dp[i] = dp[i - 1] + dp[i - 2]; | ||
} | ||
return dp[n]; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
interview_prep/algorithm/java/src/test/java/hoa/can/code/ClimbStartsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package hoa.can.code; | ||
|
||
import hoa.can.code.ez.ClimbStairs; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class ClimbStartsTest { | ||
ClimbStairs sol = new ClimbStairs(); | ||
|
||
@Test | ||
@DisplayName("https://leetcode.com/problems/climbing-stairs/") | ||
public void test() { | ||
assertEquals(0, sol.climbs(0)); | ||
assertEquals(1, sol.climbs(1)); | ||
assertEquals(3, sol.climbs(3)); | ||
} | ||
} |