-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_solution.js
56 lines (46 loc) · 1.4 KB
/
my_solution.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
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
const topKFrequent = (nums, k) => {
// [1,1,1,2,2,3]
if (k < 1) return [];
// a map of the appear nb, a list of nums length to store the list based on appr nb
let map = new Map(), kList = Array.from({ length: nums.length + 1}, () => []);
// Array(nums.length).fill([]); - This will fill the array with empty arrays but it has the same references to each item (array).
// you are actually pushing values into the same array instance across all indices.
// process the map
for (let i = 0; i < nums.length; i++) {
map.set(nums[i], (map.get(nums[i]) || 0) + 1)
}
// i will have a k list then to get from behind, (pop) if empty, nth, otherwise, K--; ⇒ 0
// [[], …]
console.log(map)
console.log(kList)
for (let [key, value] of map) {
console.log(value)
console.log(kList[value])
kList[value].push(key);
console.log("kList")
console.log(kList)
}
// [[],[3],[2],[1], …]
let result = [], tmpK = k;
while (tmpK > 0) {
let curr = kList.pop();
if (0 < curr.length) {
result.push(curr);
tmpK -= curr.length;
}
}
// [[1,2], [3]]
console.log("result")
console.log(k)
console.log(result.flat())
console.log(result.flat().slice(0, k))
return result.flat().slice(0, k);
}
// topKFrequent([1, 1, 1, 2, 2, 2, 3], 2)
// topKFrequent([1], 1)
// topKFrequent([1,2], 2)