Top K Frequent
Xem dạng PDF
Gửi bài giải
Điểm:
100,00
Giới hạn thời gian:
2.0s
Giới hạn bộ nhớ:
256M
Tác giả:
Dạng bài
Ngôn ngữ cho phép
Python
Task
Given a list of integers, return the k most frequently occurring elements. If multiple elements have the same frequency, break ties by choosing the smaller value first. Return the k elements sorted in ascending order.
Input
- Line 1: two integers
n k— number of elements and k - Line 2: n space-separated integers
Output
Print the k most frequent elements space-separated in ascending order.
Example
Input:
7 2
1 1 1 2 2 3 3
Output:
1 2
Scaffolding
Submit a Python file defining:
def top_k_frequent(nums: list[int], k: int) -> list[int]:
...
Receives a list of integers and an integer k, and returns the k most frequent elements as a sorted list (ascending), with ties broken by smaller value.
Bình luận