Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a5aeb0c

Browse files
committed
Add solution #1998
1 parent 3b1c655 commit a5aeb0c

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,678 LeetCode solutions in JavaScript
1+
# 1,679 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1533,6 +1533,7 @@
15331533
1995|[Count Special Quadruplets](./solutions/1995-count-special-quadruplets.js)|Easy|
15341534
1996|[The Number of Weak Characters in the Game](./solutions/1996-the-number-of-weak-characters-in-the-game.js)|Medium|
15351535
1997|[First Day Where You Have Been in All the Rooms](./solutions/1997-first-day-where-you-have-been-in-all-the-rooms.js)|Medium|
1536+
1998|[GCD Sort of an Array](./solutions/1998-gcd-sort-of-an-array.js)|Hard|
15361537
2000|[Reverse Prefix of Word](./solutions/2000-reverse-prefix-of-word.js)|Easy|
15371538
2011|[Final Value of Variable After Performing Operations](./solutions/2011-final-value-of-variable-after-performing-operations.js)|Easy|
15381539
2016|[Maximum Difference Between Increasing Elements](./solutions/2016-maximum-difference-between-increasing-elements.js)|Easy|
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* 1998. GCD Sort of an Array
3+
* https://leetcode.com/problems/gcd-sort-of-an-array/
4+
* Difficulty: Hard
5+
*
6+
* You are given an integer array nums, and you can perform the following operation any
7+
* number of times on nums:
8+
* - Swap the positions of two elements nums[i] and nums[j] if gcd(nums[i], nums[j]) > 1
9+
* where gcd(nums[i], nums[j]) is the greatest common divisor of nums[i] and nums[j].
10+
*
11+
* Return true if it is possible to sort nums in non-decreasing order using the above swap
12+
* method, or false otherwise.
13+
*/
14+
15+
/**
16+
* @param {number[]} nums
17+
* @return {boolean}
18+
*/
19+
var gcdSort = function(nums) {
20+
const maxNum = Math.max(...nums);
21+
const parent = new Array(maxNum + 1).fill().map((_, i) => i);
22+
const minPrime = new Array(maxNum + 1).fill(0);
23+
for (let i = 2; i <= maxNum; i++) {
24+
if (minPrime[i] === 0) {
25+
for (let j = i; j <= maxNum; j += i) {
26+
minPrime[j] = i;
27+
}
28+
}
29+
}
30+
31+
const groups = new Map();
32+
for (const num of nums) {
33+
let curr = num;
34+
const primes = new Set();
35+
while (curr > 1) {
36+
const prime = minPrime[curr];
37+
primes.add(prime);
38+
curr /= prime;
39+
}
40+
for (const prime of primes) {
41+
if (!groups.has(prime)) groups.set(prime, []);
42+
groups.get(prime).push(num);
43+
}
44+
}
45+
46+
for (const numbers of groups.values()) {
47+
for (let i = 1; i < numbers.length; i++) {
48+
union(numbers[i - 1], numbers[i], parent);
49+
}
50+
}
51+
52+
const sorted = [...nums].sort((a, b) => a - b);
53+
for (let i = 0; i < nums.length; i++) {
54+
if (find(nums[i], parent) !== find(sorted[i], parent)) {
55+
return false;
56+
}
57+
}
58+
59+
return true;
60+
61+
function find(x, parent) {
62+
if (parent[x] !== x) parent[x] = find(parent[x], parent);
63+
return parent[x];
64+
}
65+
66+
function union(x, y, parent) {
67+
parent[find(x, parent)] = find(y, parent);
68+
}
69+
};

0 commit comments

Comments
 (0)