Skip to content

Commit 4612a59

Browse files
committed
Runtime: 758 ms (Top 12.50%) | Memory: 93.1 MB (Top 12.50%)
1 parent 4b8ab77 commit 4612a59

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Runtime: 758 ms (Top 12.50%) | Memory: 93.1 MB (Top 12.50%)
2+
function gcd(a, b) {
3+
while (b > 0) {
4+
a %= b;
5+
[a, b] = [b, a];
6+
}
7+
return a;
8+
}
9+
function lcm(a, b) {
10+
return a / gcd(a, b) * b;
11+
}
12+
13+
var replaceNonCoprimes = function(nums) {
14+
let res = new Array();
15+
for (let num of nums) {
16+
while (res.length > 0 && gcd(res.at(-1), num) > 1) {
17+
num = lcm(res.at(-1), num);
18+
res.pop();
19+
}
20+
res.push(num);
21+
}
22+
return res;
23+
};

0 commit comments

Comments
 (0)