Loading
Loading
An outdoor gear shop logs every term typed into its search box. The homepage shows the k most searched terms as quick links.
Given the list terms and an integer k, return the k most frequent terms, most frequent first. When two terms were searched equally often, the one that comes first alphabetically ranks higher.
terms = [tent, stove, map, tent, map, lamp, tent], k = 2countstent 3, stove 1, map 2, lamp 1heap = [tent]size-2 min-heap, weakest at the rootheap = [stove, tent]stove (1) is the weakestheap = [map, tent]map (2) arrives, stove is evictedheap = [map, tent]lamp (1) arrives and is evicted at once[tent, map]pop weakest first, then reverse[tent, map]Sign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(n + m log k) time · O(m) space
Input. Run your code to watch it work on this input, one line at a time.
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.
tent was searched 3 times and map twice.
Example 2
boots and kayak tie at 2; alphabetical order puts boots first.