-
-
Notifications
You must be signed in to change notification settings - Fork 245
[ackku] week 2 #713
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
[ackku] week 2 #713
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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,45 @@ | ||
// dfs를 이용한 풀이 | ||
// dp 배열을 이용한 것보다 공간복잡도가 상대적으로 낮게 잡힘 40.4mb -> 40.1mb | ||
// 이유가 뭔지는 조금 더 고민해봐야할 듯 | ||
class Solution { | ||
public int climbStairs(int n) { | ||
return dfs(n, new HashMap<>()); | ||
} | ||
|
||
private int dfs(int n, Map<Integer, Integer> memo) { | ||
if (n == 0) { | ||
return 1; | ||
} | ||
if (n < 0) { | ||
return 0; | ||
} | ||
if (memo.containsKey(n)) { | ||
return memo.get(n); | ||
} | ||
|
||
int result = dfs(n - 1, memo) + dfs(n - 2, memo); | ||
memo.put(n, result); | ||
|
||
return result; | ||
} | ||
} | ||
|
||
// 가장 먼저 생각한 풀이 | ||
// 피보나치 수열의 형태의 결과물을 갖는다. | ||
// O(N)의 점화식을 세울 수 있으면 어느 방식으로도 풀림 | ||
class Solution { | ||
public int climbStairs(int n) { | ||
if(n == 1) return 1; | ||
if(n == 2) return 2; | ||
|
||
int[] dp = new int[n + 1]; | ||
dp[1] = 1; // 1 | ||
dp[2] = 2; // 1+1, 2 | ||
|
||
for (int i = 3; i <= n; i++) { | ||
dp[i] = dp[i-1] + dp[i-2]; | ||
} | ||
|
||
return dp[n]; | ||
} | ||
} |
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,33 @@ | ||
// 자바에서는 시간복잡도를 O(N)으로 잡아도 최상위권으로 갈 수 없는 문제 | ||
// 유니코드 고려 | ||
public boolean isAnagram(String s, String t) { | ||
if (s.length() != t.length()) return false; | ||
|
||
Map<Character, Integer> map = new HashMap<>(); | ||
for (int i = 0; i < s.length(); i++) { | ||
map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1); | ||
map.put(t.charAt(i), map.getOrDefault(t.charAt(i), 0) - 1); | ||
} | ||
for (int value : map.values()) { | ||
if (value != 0) return false; | ||
} | ||
return true; | ||
} | ||
// 알파벳만 고려 | ||
public boolean isAnagram(String s, String t) { | ||
int ALPHABET_COUNT = 26; | ||
if(s.length() != t.length()) { | ||
return false; | ||
} | ||
int[] arr = new int[ALPHABET_COUNT]; // 알파벳 갯수 | ||
for(int i = 0; i < s.length() ; i ++) { | ||
arr[s.charAt(i) - 97]++; | ||
arr[t.charAt(i) - 97]--; | ||
Comment on lines
+24
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 97 대신 'a' 를 쓰시면 좀 더 직관적으로 다가올것 같아요! |
||
} | ||
for(int i = 0; i < ALPHABET_COUNT; i ++) { | ||
if(arr[i] != 0) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} |
Oops, something went wrong.
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.
DP만 생각했는데 DFS 풀이가 참신한것 같습니다 :)