Skip to content

Conversation

@BangDori
Copy link
Member

문제

Type Info
Time Complexity O(N + m log m)
Space Complexity O(M)
Algorithm 우선순위
Data Structure Array, Map

Constraints

  • 1 <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4
  • k is in the range [1, the number of unique elements in the array].

Edge Case

  • 엣지 케이스를 발견하지 못했습니다.

풀이

문제의 핵심은 가장 빈번하게 나타나는 숫자 K개를 구하는 문제 (문제 이해하는 게 가장 어려웠어요 ㅠㅠ 😭)

nums배열에서 나온 숫자의 횟수를 저장할 Map 객체 생성

for (nums 배열 내부) {
  if map 객체에 이미 (num) 존재한다면
    이전에 있던 횟수 + 1 Map에 저장
  else
    [, 횟수(1)] Map에 저장
}

Map 객체를 배열로 변경하고, 횟수를 기준으로 내림차순 정렬
answer = [];

for (k만큼) {
  answer 배열에 k-1까지의 값을 저장
}

return answer

어려웠던 점

  • 없음

알게된 점

  • 없음

@asuan99 asuan99 merged commit 067e020 into main Dec 26, 2024
2 checks passed
@BangDori BangDori deleted the bangdori/top-k-frequent-elements branch December 26, 2024 13:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants