-
-
Notifications
You must be signed in to change notification settings - Fork 245
[jinhyungrhee] WEEK 02 solutions #1220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b7f97fe
add solution of valid-anagram
jinhyungrhee dc58154
Merge remote-tracking branch 'origin/main'
jinhyungrhee 3483854
add a new line
jinhyungrhee d66407a
add solution of climbing-stairs
jinhyungrhee 429ae9a
add a new line
jinhyungrhee e0b83d2
add solution of product-of-array-except-self
jinhyungrhee d6e3de9
add solution of 3sum
jinhyungrhee 9f0c962
add solution of valid-binary-search-tree
jinhyungrhee 03c56f4
add solution of valid-binary-search-tree using iterative DFS
jinhyungrhee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,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) { | ||
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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
알고 계실 수도 있으시겠지만, 동일한
Bottom-up
방식으로 O(1)의 공간 복잡도를 갖는 방식이 존재합니다 😃알고달레 참고 링크 남겨드립니다 :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
참고해보겠습니다 감사합니다!