Notice Period

Top K Bestsellers

medium

A food stall logs every order as a numeric dish id. At closing time the owner wants the k dishes that sold the most, to plan tomorrow's prep.

Given the raw log orders and an integer k, return the k most frequent dish ids in any order. The answer is guaranteed unique — no frequency ties cross the cutoff.

Example 1
in orders = [1, 1, 1, 2, 2, 3], k = 2
out [1, 2]
Dish 1 sold 3 times, dish 2 twice, dish 3 once.
Example 2
in orders = [1], k = 1
out [1]
Constraints
  • · 1 <= len(orders) <= 2000
  • · 1 <= k <= number of distinct dish ids
  • · -10^4 <= orders[i] <= 10^4
  • · the answer is unique
similar problem on LeetCode ↗
solution.pyloading python…
test results
Hit ▶ Run to test your code against the visible cases.