-
-
Notifications
You must be signed in to change notification settings - Fork 360
[JinuCheon] WEEK 02 solutions #2685
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # 재귀를 만드는 것이 잘 생각나지 않아서, 이렇게 저렇게 시도해보다 결국 LLM 도움. | ||
| # 그렇지만 제출 결과 Time Out. | ||
| # 2^N 복잡도임. max 45 의 경우 35,184,372,088,832. | ||
| class Solution: | ||
| def climbStairs(self, n: int) -> int: | ||
| # 1 -> 한칸만 가능 | ||
| # 2 -> (1,1) or (2) | ||
| if n <= 2: | ||
| return n; | ||
|
|
||
| # 1칸 진행한 케이스 + 2칸 진행한 케이스 | ||
| return self.climbStairs(n-1) + self.climbStairs(n-2); | ||
|
|
||
| # memonization 적용. 실무를 하고나서 이걸 보니, 캐싱이라고 부르고 싶다. | ||
| # 1~N 숫자 하나당 한번의 계산을 하게 되니, 시간복잡도는 O(1) 이다. | ||
| # 이게 왜 easy 난이도지? 잊어먹었다가 주말에 자력으로 풀어보자. | ||
| class Solution: | ||
| def climbStairs(self, n: int) -> int: | ||
| self.memo = {} | ||
| return self.dfs(n) | ||
|
|
||
| def dfs(self, n: int) -> int: | ||
| if n <= 2: | ||
| return n | ||
|
|
||
| # 이미 해당 수를 계산한 적이 있다면 early return. | ||
| if n in self.memo: | ||
| return self.memo[n] | ||
|
|
||
| # 새로운 결과가 있으면 무조건 저장. | ||
| self.memo[n] = self.dfs(n - 1) + self.dfs(n - 2) | ||
| return self.memo[n] |
|
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(k) |
피드백: 두 문자열의 길이가 n이라고 할 때 한 번씩 순회하며 카운트를 비교합니다. 해시맵 사용으로 추가 공간이 필요합니다.
개선 제안: 현재 구현이 효율적이며, 입력의 문자 집합이 제한된 경우 배열 기반 카운트로 더 빠르게 구현 가능.
풀이 2: Solution2.isAnagram — Time: O(n) / Space: O(k)
| 복잡도 | |
|---|---|
| Time | O(n) |
| Space | O(k) |
피드백: 카운트를 하나의 해시맵으로 관리해 메모리 사용을 줄이고, 최종 값을 확인합니다.
개선 제안: 현재 구현이 적합합니다. 필요 시 Counter 도입으로 간결화 가능.
풀이 3: Solution3.isAnagram — Time: O(n) / Space: O(k)
| 복잡도 | |
|---|---|
| Time | O(n) |
| Space | O(k) |
피드백: Counter는 내부적으로 해시맵을 사용하므로 직관적이고 구현이 간단합니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 4: Solution.isAnagram — Time: O(n log n) / Space: O(1)
| 복잡도 | |
|---|---|
| Time | O(n log n) |
| Space | O(1) |
피드백: 정렬 자체가 더 큰 시간 복잡도를 야기합니다.
개선 제안: 대부분의 경우 해시맵 기반 솔루션이 더 빠릅니다.
💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # my own solution | ||
| # O(n) | ||
| class Solution: | ||
| def isAnagram(self, s: str, t: str) -> bool: | ||
| count1 = {} | ||
| for ch in s: | ||
| count1[ch] = count1.get(ch, 0) + 1 | ||
|
|
||
| count2 = {} | ||
| for ch in t: | ||
| count2[ch] = count2.get(ch, 0) + 1 | ||
|
|
||
| return count1 == count2 | ||
|
|
||
| # gpt suggestion | ||
| ### half space | ||
| #### O(n) | ||
| class Solution2: | ||
| def isAnagram(self, s: str, t: str) -> bool: | ||
| count = {} | ||
| for ch in s: | ||
| count[ch] = count.get(ch, 0) + 1 | ||
| for ch in t: | ||
| count[ch] = count.get(ch, 0) - 1 | ||
| return all(v == 0 for v in count.values()) | ||
|
|
||
| ### simple solution (What??) | ||
| #### O(n) | ||
| #### Counter works like my own solution internally. But I'm ganna forgot this, becuase I'm learning now. | ||
| from collections import Counter | ||
| class Solution3: | ||
| def isAnagram(self, s: str, t: str) -> bool: | ||
| return Counter(s) == Counter(t) | ||
|
|
||
| ### using sort | ||
| #### O(n log n) | ||
| class Solution: | ||
| def isAnagram(self, s: str, t: str) -> bool: | ||
| return sorted(s) == sorted(t) |
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
풀이 1:
Solution.climbStairs— Time: O(2^n) / Space: O(n)피드백: 기본 재귀 구현은 지수시간이므로 대규모 입력에서 Time Limit를 초래합니다.
개선 제안: 고려해볼 만한 대안: 메모이제이션(또는 동적 프로그래밍)으로 중복 계산을 제거하면 시간 복잡도를 O(n)으로 낮출 수 있습니다.
풀이 2:
Solution.climbStairs— Time: ❌ O(1) → O(n) / Space: O(n)피드백: 해당 구현은 각 호출에서 결과를 저장하여 필요 시 재사용합니다. 시간과 공간 모두 선형 수준으로 감소합니다.
개선 제안: 현재 구현이 적절해 보입니다.