-
-
Notifications
You must be signed in to change notification settings - Fork 361
[Chanz] WEEK 02 Solutions #2695
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
b6e35e7
e026ddd
e8339c9
40cb681
5564f3d
bad1013
913950b
7ba1412
f23e05e
9fdd08f
85e32b9
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,23 @@ | ||
| class Solution: | ||
|
|
||
| def __init__(self): | ||
| self.memo = dict() | ||
|
|
||
| def climbStairs(self, n: int) -> int: | ||
|
|
||
| # 공간복잡도 : n의 개수 만큼 memo가 할당되므로 o(n) | ||
| # 시간복잡도 : 각 칸에 대해서 계산이 1번씩만 되므로 o(n) | ||
|
|
||
| # base | ||
| if n <= 2 : | ||
| return n | ||
|
|
||
| # 이미 계산 한 값이라면 반환 | ||
| if n in self.memo: | ||
| return self.memo[n] | ||
|
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칸 전과 2칸 전의 결과를 합한 것을 반환. | ||
| result = self.climbStairs(n-2) + self.climbStairs(n-1) | ||
| self.memo[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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 추가 배열 left와 right를 사용해 각 원소에 대해 O(1) 추가 연산으로 결과를 얻는다. 개선 제안: 추가 공간 없이 결과 배열 하나만 사용하도록 압축 가능하다.
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. Follow up 에 공간복잡도 O(1)로 해결해 보라는게 있어서요 ( 출력 배열 1개 제외 ) 한번 시도해 보시는것도 좋을것 같아요! 힌트는 left, right 이 두개는 사실 배열이 아니여도 됩니다! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| class Solution: | ||
| def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
|
|
||
| # 공간 복잡도 : 2개의 list를 사용하므로 O(2n)=O(n) | ||
| # 시간 복잡도 : nums를 세번 순회 o(n) | ||
| n = len(nums) | ||
| left = [1] * n | ||
| right = [1] * n | ||
|
|
||
| for idx in range(1, n): | ||
| left[idx] = left[idx-1] * nums[idx-1] | ||
|
|
||
| for idx in range(n-2, -1, -1): | ||
| right[idx] = right[idx+1] * nums[idx+1] | ||
|
|
||
| return [left[i] * right[i] for i in range(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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 두 문자열 길이에 비례하는 선형 시간과 알파벳 크기에 비례하는 공간을 사용한다. 개선 제안: 모든 문자가 ASCII인 경우 배열 인덱스 기반 구현으로 상수 공간에 근접 가능하다.
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. 처음에 string s랑 string t랑 길이가 다르면 반복문을 아예 돌지 않고 바로 false를 반환하는 base가 있으면 더 좋을 거 같아요! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| class Solution: | ||
| def isAnagram(self, s: str, t: str) -> bool: | ||
| hashmap = dict() | ||
|
|
||
| # 공간복잡도 : hashmap을 사용하므로 공간복잡도는 O(N)입니다. | ||
| # 시간복잡도 : 문자열 s의 길이만큼 한번 순회하고, 다시 문자열 t의 길이만큼 순회하므로 O(s+t) => O(N)입니다. | ||
| for ch in s: | ||
| hashmap[ch] = hashmap.get(ch, 0) + 1 | ||
|
|
||
| for ch in t: | ||
| if ch in hashmap: | ||
| hashmap[ch] -= 1 | ||
| if hashmap[ch] == 0: | ||
| del hashmap[ch] | ||
| else: | ||
| return False | ||
|
|
||
| return not hashmap | ||
|
|
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 각 n에 대해 한 번씩 계산하고 결과를 메모에 저장하므로 시간은 선형이며, 재귀 깊이는 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.
공간복잡도 맞긴 하신데, 재귀를 하면 메모리 스택에 Function call이 계속 쌓이기 때문에 배열을 쓰는것보단 좋진 않을거에요!
쉬운것부터 바텀업으로 구현하시는것도 저는 DP를 공부하기 좋은 선택이라고 생각합니다.
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.
아아 그러네요 스택 메모리도 감안을 해야 하는군요.. 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.
저도 앞에 피드백 주신 분과 같이 n이 커지면 recursion error가 날 수 도 있을 거 같아서 바텀업 구현을 고려해보시는 것도 좋을 거 같다는 의견입니다!