-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path062-cubic-permutations.js
More file actions
41 lines (33 loc) · 998 Bytes
/
062-cubic-permutations.js
File metadata and controls
41 lines (33 loc) · 998 Bytes
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
/**
* Cubes Permutations
* Time Complexity: O(N * D log D)
* Space Complexity: O(N)
*/
function processData(input) {
let [N, K] = input.trim().split(/\s+/).map(Number);
const map = new Map();
for (let a = 1; a < N; a++) {
const cube = BigInt(a) * BigInt(a) * BigInt(a);
const sig = cube.toString().split('').sort().join('');
if (!map.has(sig)) map.set(sig, []);
map.get(sig).push(cube);
}
const answers = [];
for (let [sig, list] of map.entries()) {
if (list.length === K) {
list.sort((x, y) => (x < y ? -1 : x > y ? 1 : 0));
answers.push(list[0]);
}
}
answers.sort((x, y) => (x < y ? -1 : x > y ? 1 : 0));
console.log(answers.map(v => v.toString()).join("\n"));
};
process.stdin.resume();
process.stdin.setEncoding("ascii");
let _input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});