Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions climbing-stairs/jinhyungrhee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import java.util.*;
class Solution {
public int climbStairs(int n) {

// METHOD1 : recursive DFS
// int resuslt = recursiveDFS(0, n);

// METHOD2 : recursive DFS + memoization
// int[] memo = new int[n + 1];
// Arrays.fill(memo, -1);
// int result = memoizationDFS(0, n, memo);

// METHOD3 : DP
int[] memo = new int[n + 1];
Arrays.fill(memo, -1);
int result = dp(n, memo);

return result;
}

/**
runtime : 0ms
memory : 40.04mb
*/

// METHOD3 : DP (Bottom-Up)
// time-complexity : O(N)
// space-complexity : O(N)
public int dp(int n, int[] memo) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

알고 계실 수도 있으시겠지만, 동일한 Bottom-up 방식으로 O(1)의 공간 복잡도를 갖는 방식이 존재합니다 😃
알고달레 참고 링크 남겨드립니다 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

참고해보겠습니다 감사합니다!

if (n <= 2) return n;
memo[1] = 1;
memo[2] = 2;
for (int i = 3; i < n + 1; i++) {
memo[i] = memo[i - 1] + memo[i - 2];
}
return memo[n];
}

/**
runtime : 0ms
memory : 40.30mb
*/

// METHOD2 : DFS + memoization (Top-Down)
// time-complexity : O(N) -> 각 i에 대해 dfs(i)는 최대 한번만 호출됨
// space-complexity : O(N)
public int memoizationDFS(int i, int n, int[] memo) {
if (i > n) return 0;
if (i == n) return 1;
if (memo[i] != -1) return memo[i];
memo[i] = memoizationDFS(i + 1 , n, memo) + memoizationDFS(i + 2, n, memo);
return memo[i];
}

/**
Time Limit Exceeded
*/

// METHOD1 : recursive DFS => TIME-OUT 발생
// time-complexity : O(2^N) -> n이 커질수록 중복 호출 발생
// space-complexity : O(N)
public int recursiveDFS(int i, int n) {
if (i > n) return 0;
if (i == n) return 1;
return recursiveDFS(i + 1, n) + recursiveDFS(i + 2, n);
}
}