Skip to content

q 31 . 상위 k빈도 요소 관련해서 질문 #136

Open

Description

@goodsosbva

k번 만큼 추출, 최소 힙(Min Heap)이므로 가장 작은 음수 순으로 추출

for _ in range(k):
    topk.append(heapq.heappop(freqsHeap)[1])

여기서 k번 만큼 추출하는데 , 문제에서 원하는건 k번 등장하는 요소를 추출하는거와는 조금 다르지 않나요?
예를 들면 입력에서
nums = [1, 1, 1, 2, 2, 3] -> nums = [1, 1, 1, 2, 2, 3, 3] 변경하면 원하는 출력은
[1, 2, 3] 이 되야하는데 위에 언급된 방식은 k번 까지만 추출해서 [1, 2]가 나오는데요..
제가 뭘 잘못 이해한건가요?

Activity

  1. likejazz commented on Jan 9, 2022

    @likejazz
    Collaborator

    책에는 문제의 제약 조건까지 구체적으로 명시하지 않았으나 리트코드의 문제 설명을 보면 하단에 제약사항이 나열되어 있습니다.

    • 1 <= nums.length <= 10^5
    • k is in the range [1, the number of unique elements in the array].
    • It is guaranteed that the answer is unique.

    여기를 보면 유니크 하다는 제약을 두고 있기 때문에 따라서 동일한 빈도 수는 존재하지 않습니다.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions