Skip to content

Conversation

haklee
Copy link
Contributor

@haklee haklee commented Sep 8, 2024

답안 제출 문제

체크 리스트

  • PR을 프로젝트에 추가하고 Week를 현재 주차로 설정해주세요.
  • 바로 앞에 PR을 열어주신 분을 코드 검토자로 지정해주세요.
  • 문제를 모두 푸시면 프로젝트에서 Status를 In Review로 설정해주세요.
  • 코드 검토자 1분 이상으로부터 승인을 받으셨다면 PR을 병합해주세요.

@haklee haklee requested a review from a team as a code owner September 8, 2024 23:29
@github-actions github-actions bot added the py label Sep 8, 2024
@haklee haklee requested a review from tolluset September 8, 2024 23:30
def maxProfit(self, prices: List[int]) -> int:
minp, profit = prices[0], 0
for p in prices:
profit = max(profit, p - (minp := min(minp, p)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

d = {}
for s in strs:
# k값 계산이 오른쪽에서 먼저 이루어지는군요?!
d[k] = d.get(k := tuple(sorted(s)), []) + [s]
Copy link
Member

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(새로운 애너그램)

위와 같이 그냥 사전에 들어있는 기존 리스트에 새로운 애너그램을 추가하도록 구현을 할 수도 있을 것 같아요.

이 두 가지 방식이 메모리 효율 측면에서 유의미한 차이가 있을까요?

Copy link
Contributor

@obzva obzva Sep 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

궁금해서 확인해보니까 append, +=는 해당 list를 그대로 둔 채로 새 원소를 추가해주는 것 같고 b = b + [..] 방식으로 list를 이어줄 경우엔 아예 새로운 list를 할당해주는 것 같네요

- (SC) 매 계산마다 최대 한 번 solution을 추가하는 연산을 한다.
- 그러므로 각 순회마다 C * (n-1), C * (n-2), ..., C * 1의 시간이 들어감.
- (SC) 비슷하게, 매 순회마다 위와 같은 꼴로 solution 개수가 더해질 수 있다.
- 종합하면 O(n^2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

설명을 꼼꼼하게 작성해주셔서 코드와 함께 따라가기 정말 좋습니다~
한 가지 알고 있는 부분을 말씀드리면, 복잡도를 분석할 때 output 에 필요한 메모리는 보통 무시하는 것 같습니다!

Copy link
Contributor

@tolluset tolluset left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생하셨습니다!!

Comment on lines +38 to +45
# 커팅. 어차피 세 쌍의 숫자에 등장할 수 있는 같은 숫자 개수가 최대 3개이므로,
# 처음 주어진 nums에 같은 숫자가 네 번 이상 등장하면 세 번만 나오도록 바꿔준다.
# 이 처리를 하면 같은 숫자가 많이 반복되는 케이스에서 시간 개선이 있을 수 있다.
# Counter 쓰는 데에 O(n), 새로 tmp_nums 리스트를 만드는 데에 O(n)의 시간이 들어가므로
# 최종적인 시간 복잡도에 영향을 주지는 않는다.
tmp_nums = []
for k, v in Counter(nums).items():
tmp_nums += [k] * min(v, 3)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

전처리 배워갑니다 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이전 문제의 trie 응용한거 좋네요! 👍

Copy link
Contributor

@bky373 bky373 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생하셨습니다~

@leokim0922
Copy link
Contributor

너무 오랜만에 왔더니 학님 솔루션 인기가 대단하네요 ㅋㅋ 이번에도 배우고 갑니다! 고생하셨어요 👍

@haklee haklee merged commit 2ff5051 into DaleStudy:main Sep 15, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
Status: Completed
Development

Successfully merging this pull request may close these issues.

6 participants