Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
45 changes: 45 additions & 0 deletions climbing-stairs/imsosleepy.java
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;
}
Comment on lines +5 to +24
Copy link
Contributor

Choose a reason for hiding this comment

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

DP만 생각했는데 DFS 풀이가 참신한것 같습니다 :)

}

// 가장 먼저 생각한 풀이
// 피보나치 수열의 형태의 결과물을 갖는다.
// 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];
}
}
33 changes: 33 additions & 0 deletions valid-anagram/imsosleepy.java
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
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
}
Loading