Skip to content

[doh6077] WEEK 01 solutions#2370

Open
doh6077 wants to merge 6 commits intoDaleStudy:mainfrom
doh6077:main
Open

[doh6077] WEEK 01 solutions#2370
doh6077 wants to merge 6 commits intoDaleStudy:mainfrom
doh6077:main

Conversation

@doh6077
Copy link
Contributor

@doh6077 doh6077 commented Mar 3, 2026

답안 제출 문제

작성자 체크 리스트

  • Projects의 오른쪽 버튼(▼)을 눌러 확장한 뒤, Week를 현재 주차로 설정해주세요.
  • 문제를 모두 푸시면 프로젝트에서 StatusIn Review로 설정해주세요.
  • 코드 검토자 1분 이상으로부터 승인을 받으셨다면 PR을 병합해주세요.

검토자 체크 리스트

Important

본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!

  • 바로 이전에 올라온 PR에 본인을 코드 리뷰어로 추가해주세요.
  • 본인이 검토해야하는 PR의 답안 코드에 피드백을 주세요.
  • 토요일 전까지 PR을 병합할 수 있도록 승인해주세요.

@ykim7 ykim7 self-requested a review March 4, 2026 01:30
@doh6077 doh6077 moved this from Solving to In Review in 리트코드 스터디 7기 Mar 6, 2026
Copy link
Contributor

@ykim7 ykim7 left a comment

Choose a reason for hiding this comment

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

set을 이용하여 중복된 객체가 있어서 길이가 줄어든 경우, 그것을 비교하여 중복여부를 확인한 것이 좋았습니다. 코드가 짧고 읽기 쉽다고 생각합니다.
지금도 충분하지만, 중간에 중복을 발견하면 조기 종료하는 방식도 고려할 수 있을 것 같습니다.

Copy link
Contributor

@ykim7 ykim7 left a comment

Choose a reason for hiding this comment

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

해시맵을 사용하여 다른 방법에 비해 시간 복잡도를 O(n)으로 줄여서 효율적으로 풀이했다고 생각합니다.

# use Hash map to save num and index
nums_hm = {}

for i, num in enumerate(nums):
Copy link
Contributor

Choose a reason for hiding this comment

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

enumerate를 사용해서 index와 num을 동시에 가져온 점이 좋았습니다.

Copy link
Contributor

@ykim7 ykim7 left a comment

Choose a reason for hiding this comment

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

단계별로 읽기 쉽고 이해가 잘 되는 코드였습니다.
다만 freq를 생성하는 과정은 O(n)이지만, 이후 sorted()를 사용하면서 정렬에 O(m log m)이 추가로 발생하는 것 같습니다. 빈도를 인덱스로 사용하는 bucket 구조를 활용하면 O(n)에 가깝게 풀 수도 있을 것 같습니다. 예를 들어 빈도: 숫자 리스트 형태로 저장한 뒤, 높은 빈도부터 탐색하는 방식도 하나의 대안이 될 수 있을 것 같습니다.

freq[num] += 1

# 2. Sort by frequency in descending order
sorted_items = sorted(freq.items(), key=lambda item: item[1], reverse=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

sorted를 사용할 때 reverse 옵션 외에 key 옵션을 넣어 값을 비교할 수 있다는 점을 알았습니다.

Copy link
Contributor

@ykim7 ykim7 left a comment

Choose a reason for hiding this comment

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

전반적으로 흐름이 잘 보이고 이해하기 쉬운 풀이였습니다. 다만 이 문제에서는 시간 복잡도를 얘기하고 있어 그 관점에서 다시 살펴보았습니다. 현재 코드는 sort()를 사용하면서 O(n log n)의 시간 복잡도가 발생하고, 이후 while loop는 O(n)이므로 전체적으로 O(n log n)이 되는 것 같습니다.
정렬 대신 이전 문제에서 사용하셨던 set을 활용하는 방법도 생각해 볼 수 있을 것 같습니다. set은 중복을 제거하고, 있는지 없는지를 확인하는 탐색이 O(1)이기 때문에 이를 이용하면 전체 시간 복잡도를 O(n)에 가깝게 줄일 수 있을 것 같습니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: In Review

Development

Successfully merging this pull request may close these issues.

2 participants