forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGCD Sort of an Array.js
42 lines (39 loc) · 1.23 KB
/
GCD Sort of an Array.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
// Runtime: 1815 ms (Top 40.00%) | Memory: 69.6 MB (Top 20.00%)
var gcdSort = function(nums) {
// group numbers by their prime factors
const unionFind = {};
const find = (x) => unionFind[x] = unionFind[x] === x ? x : find(unionFind[x]);
const union = (x, y) => unionFind[find(y)] = unionFind[find(x)];
for (const num of nums) {
if (!unionFind[num]) {
unionFind[num] = num;
}
const primeFactors = getPrimeFactors(num);
for (const primeFactor of primeFactors) {
if (!unionFind[primeFactor]) {
unionFind[primeFactor] = primeFactor;
}
union(primeFactor, num);
}
}
// see if it's possible to swap nums to equal sorted by checking the groups
const sortedNums = [...nums].sort((a, b) => a - b);
return nums.every((num, i) => find(num) === find(sortedNums[i]));
};
function getPrimeFactors(n) {
const primeFactors = new Set();
while (n % 2 === 0) {
primeFactors.add(2);
n /= 2;
}
for (let i = 3; i * i <= n; i += 2) {
while (n % i === 0) {
primeFactors.add(i);
n /= i;
}
}
if (n > 1) {
primeFactors.add(n);
}
return primeFactors;
}