-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1998-gcd-sort-of-an-array.js
More file actions
69 lines (62 loc) · 1.79 KB
/
1998-gcd-sort-of-an-array.js
File metadata and controls
69 lines (62 loc) · 1.79 KB
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
/**
* 1998. GCD Sort of an Array
* https://leetcode.com/problems/gcd-sort-of-an-array/
* Difficulty: Hard
*
* You are given an integer array nums, and you can perform the following operation any
* number of times on nums:
* - Swap the positions of two elements nums[i] and nums[j] if gcd(nums[i], nums[j]) > 1
* where gcd(nums[i], nums[j]) is the greatest common divisor of nums[i] and nums[j].
*
* Return true if it is possible to sort nums in non-decreasing order using the above swap
* method, or false otherwise.
*/
/**
* @param {number[]} nums
* @return {boolean}
*/
var gcdSort = function(nums) {
const maxNum = Math.max(...nums);
const parent = new Array(maxNum + 1).fill().map((_, i) => i);
const minPrime = new Array(maxNum + 1).fill(0);
for (let i = 2; i <= maxNum; i++) {
if (minPrime[i] === 0) {
for (let j = i; j <= maxNum; j += i) {
minPrime[j] = i;
}
}
}
const groups = new Map();
for (const num of nums) {
let curr = num;
const primes = new Set();
while (curr > 1) {
const prime = minPrime[curr];
primes.add(prime);
curr /= prime;
}
for (const prime of primes) {
if (!groups.has(prime)) groups.set(prime, []);
groups.get(prime).push(num);
}
}
for (const numbers of groups.values()) {
for (let i = 1; i < numbers.length; i++) {
union(numbers[i - 1], numbers[i], parent);
}
}
const sorted = [...nums].sort((a, b) => a - b);
for (let i = 0; i < nums.length; i++) {
if (find(nums[i], parent) !== find(sorted[i], parent)) {
return false;
}
}
return true;
function find(x, parent) {
if (parent[x] !== x) parent[x] = find(parent[x], parent);
return parent[x];
}
function union(x, y, parent) {
parent[find(x, parent)] = find(y, parent);
}
};