-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtop_k_frequent_words.js
74 lines (64 loc) · 2.44 KB
/
top_k_frequent_words.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// https://leetcode.com/problems/top-k-frequent-words/description/
// 692. Top K Frequent Words
/**
* @param {string[]} words
* @param {number} k
* @return {string[]}
*/
var topKFrequentWords = function(words, k = 0) {
const cnt = new Map();
for (const word of words) {
cnt.set(word, (cnt.get(word) || 0) + 1);
}
return [...cnt.entries()]
.sort((a, b) => b[1] !== a[1] ? b[1] - a[1] : a[0].localeCompare(b[0]))
.slice(0, k===0 ? words.length : Math.abs(k))
.map(item => item[0]);
};
console.log("Top K Frequent Words");
console.log(topKFrequentWords(["i","love","leetcode","i","love","coding"], 2));
// Output: ["i","love"]
console.log(topKFrequentWords(["the","day","is","sunny","the","the","the","sunny","is","is"], 4));
// Output: ["the","is","sunny","day"]
console.log(topKFrequentWords(["the","the","the","day","sunny","sunny","is","is","is"], 2))
// Output: ["is", "the"]
console.log(topKFrequentWords(["the","the","the","day","sunny","sunny","is","is","is"], 6))
// Output: ["is", "the", "sunny", "day"]
console.log(topKFrequentWords(["that","event","a","was","funny","that","that","that","funny","a","was","was"]));
// Output: ["that","was","a","funny","event"]
// ###################
// https://leetcode.com/problems/top-k-frequent-elements/description/
// 347. Top K Frequent Elements
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
var topKFrequentElements = function(nums, k) {
const cnt = new Map();
for (const num of nums) {
cnt.set(num, (cnt.get(num) || 0) + 1);
}
return [...cnt.entries()]
.sort((a, b) => b[1] == a[1] ? a[0] - b[0] : b[1] - a[1])
.slice(0, k===0 ? nums.length : Math.abs(k))
.map(item => item[0]);
};
console.log("Top K Frequent Elements");
console.log(topKFrequentElements([1,1,1,2,2,3], 2))
// Output: [1,2]
console.log(topKFrequentElements([10,11,13,25,22,30,42,10,], 2))
// Output: [10, 11]
console.log(topKFrequentElements([1], 1))
// Output: [1]
console.log(topKFrequentElements([10,11,13,25,22,30,42,10], 3))
// Output: [10, 11, 13]
console.log(topKFrequentElements([10,11,13,25,22,30,42,10], 0))
// Output: [10, 11, 13, 22, 25, 30, 42]
console.log(topKFrequentElements([10,11,13,25,22,30,42,10], -2))
// Output: [10, 11]
console.log(topKFrequentElements([1,1,1,2,2,3,0,0,0,5,5,5], 2))
// Output: [0, 1]
console.log(topKFrequentElements([1,1,1,2,2,3,5,5,5,0,0,0], 2))
// Output: [0, 1]
module.exports = { topKFrequentWords, topKFrequentElements };