Skip to content

[bhyun-kim, 김병현] Week 4 solutions #88

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

Merged
merged 5 commits into from
May 26, 2024
Merged

Conversation

bhyun-kim
Copy link
Contributor

No description provided.

- Append the sum to the output list
- Return the output list

Time complexity: O(nlogn)
Copy link
Member

Choose a reason for hiding this comment

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

빨리 제출하셨으니, 혹시 $O(n)$으로 해결할 수 있는 방법이 있을지도 고민해보시면 좋을 것 같습니다.

Copy link
Contributor

Choose a reason for hiding this comment

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

확실히 이 문제는 보통 nlog(n)으로 먼저 푸시는 분들이 많군요.

Copy link
Member

@DaleSeo DaleSeo May 23, 2024

Choose a reason for hiding this comment

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

네, $O(n log n)$ 코드도 OA에서는 통과할 수 있는 충분히 좋은 솔루션으로 생각합니다. 하지만 on-site나 화상 인터뷰에서는 면접관의 성향의 따라서 시간이 되는 경우 지원자가 최적 알고리즘을 찾도록 압박하는 경우도 있어서 피드백 드렸습니다.

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 Author

Choose a reason for hiding this comment

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

Dynamic Programming을 이용해서 $O(n)$ 솔루션으로 고쳐봤습니다.

for s in strs:
count_s = "".join(sorted(s))
if count_s in hash_table:
idx = hash_table.index(count_s)
Copy link
Member

Choose a reason for hiding this comment

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

여기서 인덱스 검색하는 것이 시간 복잡도에 어떤 영향을 줄지 생각해보셨나요?

Copy link
Contributor

@SamTheKorean SamTheKorean May 24, 2024

Choose a reason for hiding this comment

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

내장함수를 사용할 때 특히 반복문에 넣을 때는 �함수 내부의 시간복잡도 분석이 중요할 것 같습니다.

Copy link
Contributor Author

@bhyun-kim bhyun-kim May 26, 2024

Choose a reason for hiding this comment

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

image

Array와 Hash Table의 Time Complexity에 대해서 한 번 찾아보게 됬네요. 좀 더 깊이 있게 볼 내용들이 있는 것 같습니다. Hash Table을 쓴다면, (앞에서는 변수명은 hash_table이라 해두고 list를 썻네요.) search time complexity의 평균이 $O(1)$이라고 하네요.

image

이렇게 반영해서 알고리즘 고쳤더니 리트코드에서 테스트 할 때 확실히 속도도 이전 대비 빨라진 것을 확인했습니다.

Copy link
Contributor

@SamTheKorean SamTheKorean left a comment

Choose a reason for hiding this comment

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

내장함수 메서드별로 시간복잡도도 머릿속에 넣어두는게 좋겠군요! 고생하셨습니다!

@SamTheKorean SamTheKorean merged commit 9e77c59 into DaleStudy:main May 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants