-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
99 additions
and
11 deletions.
There are no files selected for viewing
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
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
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
88 changes: 88 additions & 0 deletions
88
leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/ClimbingStairs.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,88 @@ | ||
package LeetCodeJava.DynamicProgramming; | ||
|
||
// https://leetcode.com/problems/climbing-stairs/ | ||
|
||
import java.util.*; | ||
|
||
public class ClimbingStairs { | ||
|
||
// V0 | ||
// IDEA : DP | ||
public int climbStairs(int n) { | ||
|
||
// null check | ||
if (n <= 0){ | ||
return 0; | ||
} | ||
|
||
if (n == 1 || n == 2){ | ||
return n; | ||
} | ||
|
||
// NOTE !!! init dp as ArrayList | ||
List<Integer> dp = new ArrayList<>(); | ||
dp.add(1); // i = 0 | ||
dp.add(1); // i = 1 | ||
//System.out.println("dp = " + dp); | ||
|
||
// NOTE !!! start from i = 2 | ||
for (int i = 2; i <= n; i++){ | ||
int tmp = dp.get(i-1) + dp.get(i-2); | ||
dp.add(tmp); | ||
} | ||
|
||
return dp.get(n); | ||
} | ||
|
||
// V1 | ||
// IDEA : Memoization | ||
// https://leetcode.com/problems/climbing-stairs/solutions/3708750/4-method-s-beat-s-100-c-java-python-beginner-friendly/ | ||
public int climbStairs_2(int n) { | ||
Map<Integer, Integer> memo = new HashMap<>(); | ||
return climbStairs(n, memo); | ||
} | ||
|
||
private int climbStairs(int n, Map<Integer, Integer> memo) { | ||
if (n == 0 || n == 1) { | ||
return 1; | ||
} | ||
if (!memo.containsKey(n)) { | ||
memo.put(n, climbStairs(n-1, memo) + climbStairs(n-2, memo)); | ||
} | ||
return memo.get(n); | ||
} | ||
|
||
// V2 | ||
// IDEA : Tabulation | ||
// https://leetcode.com/problems/climbing-stairs/solutions/3708750/4-method-s-beat-s-100-c-java-python-beginner-friendly/ | ||
public int climbStairs_4(int n) { | ||
if (n == 0 || n == 1) { | ||
return 1; | ||
} | ||
|
||
int[] dp = new int[n+1]; | ||
dp[0] = dp[1] = 1; | ||
|
||
for (int i = 2; i <= n; i++) { | ||
dp[i] = dp[i-1] + dp[i-2]; | ||
} | ||
return dp[n]; | ||
} | ||
|
||
// V3 | ||
// IDEA : Space Optimization | ||
// https://leetcode.com/problems/climbing-stairs/solutions/3708750/4-method-s-beat-s-100-c-java-python-beginner-friendly/ | ||
public int climbStairs_5(int n) { | ||
if (n == 0 || n == 1) { | ||
return 1; | ||
} | ||
int prev = 1, curr = 1; | ||
for (int i = 2; i <= n; i++) { | ||
int temp = curr; | ||
curr = prev + curr; | ||
prev = temp; | ||
} | ||
return curr; | ||
} | ||
|
||
} |