-
-
Notifications
You must be signed in to change notification settings - Fork 361
[essaysir] WEEK 02 Solutions #2681
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Contributor
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
풀이 1:
|
| 복잡도 | |
|---|---|
| Time | O(n) |
| Space | O(n) |
피드백: 메모이제이션(Map)으로 중복 계산을 제거하여 선형 시간 복잡도와 선형 공간 복잡도를 가진다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2: Solution.isAnagram — Time: O(n + m) / Space: O(k)
| 복잡도 | |
|---|---|
| Time | O(n + m) |
| Space | O(k) |
피드백: 두 문자열의 길이에 비례하는 시간과 문자 종류 만큼의 공간을 사용한다. 맵을 이용한 구현이다.
개선 제안: 현재 구현이 적절해 보입니다.
💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!
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.
오 이제 적절한 시간복잡도네요!
정답이 피보나치 수열인것까지 아신다면 공간복잡도도 훨씬 줄이실수 있으실것 같아요!
고생하셨습니다.
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를 쓰는 문제의 경우 Top-down이 생각해내긴 훨씬 쉽지만 몇몇 문제들은 시간제한이 애매하게 걸려 있어서
재귀 호출할때 오버헤드가 생기는것 때문에 TLE가 될수도 있기 때문에
Bottom-up으로 변환하는것도 연습해보시면 좋을거에요!
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import java.util.*; | ||
|
|
||
| class Solution { | ||
| // TC: O(2의 N승) | ||
| // SC: O(N) | ||
| public static Map<Integer, Integer> memo = new HashMap<>(); | ||
|
|
||
| public int climbStairs(int n) { | ||
| // 1과 2로만 움직일 수 있을 때, 도달할 수 있는 모든 방법의 수에 대해 구하시오 | ||
| // DPS (QUEUE) , BPS (STACK) | ||
| return dfs(n); | ||
| } | ||
|
|
||
| // dfs(5) -> dfs(3) + dfs(4) -> dfs(2) + dfs(1) + dfs(3) + dfs(2) | ||
| public static int dfs(int n){ | ||
| if ( n == 1) return 1; | ||
| if ( n == 2) return 2; | ||
| if ( memo.containsKey(n) ) return memo.get(n); | ||
|
|
||
| int result = dfs(n-1) + dfs(n-2); | ||
| memo.put(n, result); | ||
| return result; | ||
| } | ||
| } |
|
Contributor
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. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import java.util.*; | ||
|
|
||
| class Solution { | ||
| public boolean isAnagram(String s, String t) { | ||
| // 둘이 anagram 이면 인지 아닌지 확인 해라 | ||
| // 아나 그램이 다시 만들 수 있는 가 == 들어있는 알파벳의 갯수가 동일한 가 | ||
| Map<Character,Integer> prevMap = new HashMap<>(); | ||
| Map<Character,Integer> curMap = new HashMap<>(); | ||
|
|
||
| for ( int i = 0; i < s.length(); i++ ){ | ||
| prevMap.merge(s.charAt(i), 1, Integer::sum); | ||
| } | ||
|
|
||
| for ( int i = 0; i < t.length(); i ++){ | ||
| curMap.merge(t.charAt(i),1 ,Integer::sum); | ||
| } | ||
|
|
||
| return prevMap.equals(curMap); | ||
| } | ||
| } |
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.
제 생각에 dfs 함수에 적절한 cache만 추가해도 시간복잡도가 엄청나게 좋아질것 같네요 ( O(N) )!
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.
한 번 그렇게 수정해보도록 하겠습니다!! 감사합니다!!