-
Notifications
You must be signed in to change notification settings - Fork 126
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
[haklee] week 5 #441
[haklee] week 5 #441
Conversation
def maxProfit(self, prices: List[int]) -> int: | ||
minp, profit = prices[0], 0 | ||
for p in prices: | ||
profit = max(profit, p - (minp := min(minp, p))) |
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.
d = {} | ||
for s in strs: | ||
# k값 계산이 오른쪽에서 먼저 이루어지는군요?! | ||
d[k] = d.get(k := tuple(sorted(s)), []) + [s] |
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.
파이썬 코드가 함축적이라서 공간 복잡도 분석이 좀 햇갈려서 질문드려요. 좀 풀어서 보면...
d[k] = [애너그램1, 애너그램2, ..., 애너그램n] + [새로운 애너그램]
위와 같이 리스트 두 개를 더해서 새로운 리스트를 만들어서 사전에 들어있는 기존 리스트를 덮어쓰도록 구현을 하셨는데요.
[애너그램1, 애너그램2, ..., 애너그램n].append(새로운 애너그램)
# 즉, d[k].append(새로운 애너그램)
위와 같이 그냥 사전에 들어있는 기존 리스트에 새로운 애너그램을 추가하도록 구현을 할 수도 있을 것 같아요.
이 두 가지 방식이 메모리 효율 측면에서 유의미한 차이가 있을까요?
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.
- (SC) 매 계산마다 최대 한 번 solution을 추가하는 연산을 한다. | ||
- 그러므로 각 순회마다 C * (n-1), C * (n-2), ..., C * 1의 시간이 들어감. | ||
- (SC) 비슷하게, 매 순회마다 위와 같은 꼴로 solution 개수가 더해질 수 있다. | ||
- 종합하면 O(n^2) |
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.
설명을 꼼꼼하게 작성해주셔서 코드와 함께 따라가기 정말 좋습니다~
한 가지 알고 있는 부분을 말씀드리면, 복잡도를 분석할 때 output 에 필요한 메모리는 보통 무시하는 것 같습니다!
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.
고생하셨습니다!!
# 커팅. 어차피 세 쌍의 숫자에 등장할 수 있는 같은 숫자 개수가 최대 3개이므로, | ||
# 처음 주어진 nums에 같은 숫자가 네 번 이상 등장하면 세 번만 나오도록 바꿔준다. | ||
# 이 처리를 하면 같은 숫자가 많이 반복되는 케이스에서 시간 개선이 있을 수 있다. | ||
# Counter 쓰는 데에 O(n), 새로 tmp_nums 리스트를 만드는 데에 O(n)의 시간이 들어가므로 | ||
# 최종적인 시간 복잡도에 영향을 주지는 않는다. | ||
tmp_nums = [] | ||
for k, v in Counter(nums).items(): | ||
tmp_nums += [k] * min(v, 3) |
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.
이전 문제의 trie 응용한거 좋네요! 👍
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.
고생하셨습니다~
너무 오랜만에 왔더니 학님 솔루션 인기가 대단하네요 ㅋㅋ 이번에도 배우고 갑니다! 고생하셨어요 👍 |
답안 제출 문제
체크 리스트
In Review
로 설정해주세요.