forked from BigEggStudy/LeetCode-CS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0692-TopKFrequentWords.cs
48 lines (42 loc) · 1.46 KB
/
0692-TopKFrequentWords.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//-----------------------------------------------------------------------------
// Runtime: 272ms
// Memory Usage: 33.9 MB
// Link: https://leetcode.com/submissions/detail/365321159/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
using System.Linq;
namespace LeetCode
{
public class _0692_TopKFrequentWords
{
public IList<string> TopKFrequent(string[] words, int k)
{
var counts = new Dictionary<string, int>();
foreach (var word in words)
{
if (!counts.ContainsKey(word))
counts[word] = 1;
else
counts[word]++;
}
var heap = new SortedDictionary<(string word, int count), int>(Comparer<(string word, int count)>.Create((a, b) =>
{
var result = a.count.CompareTo(b.count);
if (result == 0)
result = -a.word.CompareTo(b.word);
return result;
}));
foreach (var word in counts.Keys)
{
heap[(word, counts[word])] = 1;
if (heap.Count > k)
heap.Remove(heap.Keys.First());
}
var results = new List<string>();
foreach ((string word, int count) in heap.Keys)
results.Add(word);
results.Reverse();
return results;
}
}
}