-
-
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. 오 이제 적절한 시간복잡도네요!
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. 이런 DP를 쓰는 문제의 경우 Top-down이 생각해내긴 훨씬 쉽지만 몇몇 문제들은 시간제한이 애매하게 걸려 있어서
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) |
피드백: 중복 재귀를 메모이제이션으로 제거하여 각 n에 대해 한 번만 계산하도록 구현했습니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2: Solution.productExceptSelf — Time: O(n) / Space: O(1)
| 복잡도 | |
|---|---|
| Time | O(n) |
| Space | O(1) |
피드백: 하나의 배열과 상수 공간으로 각 위치의 곱을 계산합니다. 제약 조건에 따라 0의 위치 처리도 포함합니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 3: Solution.isAnagram — Time: O(n + m) / Space: O(k)
| 복잡도 | |
|---|---|
| Time | O(n + m) |
| Space | O(k) |
피드백: 두 맵의 빈도수를 비교해 anagram 여부를 판단합니다.
개선 제안: 현재 구현이 적절해 보입니다.
💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!
| 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,26 @@ | ||
| class Solution { | ||
| public int[] productExceptSelf(int[] nums) { | ||
| // 시간 복잡도가 O(N) 이어야 함. | ||
| // 배열에서 나 자신을 빼고서 모두를 곱하라 | ||
| // 어떻게 해야 시간 복잡도가 O(N) 이지 ? | ||
| int n = nums.length; | ||
| int zeroCount = 0; | ||
| int maxTotal = 1; // 0이 아닌 값들의 곱 | ||
| for (int x : nums) { | ||
| if (x == 0) zeroCount++; | ||
| else maxTotal *= x; | ||
| } | ||
|
|
||
| int[] result = new int[n]; | ||
| for (int i = 0; i < n; i++) { | ||
| if (zeroCount >= 2) { | ||
| result[i] = 0; // 0이 2개 이상 → 무조건 0 | ||
| } else if (zeroCount == 1) { | ||
| result[i] = (nums[i] == 0) ? maxTotal : 0; // 0 위치만 살아남음 | ||
| } else { | ||
| result[i] = maxTotal / nums[i]; // 0 없음 → 그냥 나눔 | ||
| } | ||
| } | ||
| 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.
한 번 그렇게 수정해보도록 하겠습니다!! 감사합니다!!