Skip to content

Commit fadf863

Browse files
committed
[LeetCode Sync] Runtime - 165 ms (33.33%), Memory - 31.4 MB (87.10%)
1 parent f80c90d commit fadf863

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<p>You are given an array of integers <code>nums</code>. Perform the following steps:</p>
2+
3+
<ol>
4+
<li>Find <strong>any</strong> two <strong>adjacent</strong> numbers in <code>nums</code> that are <strong>non-coprime</strong>.</li>
5+
<li>If no such numbers are found, <strong>stop</strong> the process.</li>
6+
<li>Otherwise, delete the two numbers and <strong>replace</strong> them with their <strong>LCM (Least Common Multiple)</strong>.</li>
7+
<li><strong>Repeat</strong> this process as long as you keep finding two adjacent non-coprime numbers.</li>
8+
</ol>
9+
10+
<p>Return <em>the <strong>final</strong> modified array.</em> It can be shown that replacing adjacent non-coprime numbers in <strong>any</strong> arbitrary order will lead to the same result.</p>
11+
12+
<p>The test cases are generated such that the values in the final array are <strong>less than or equal</strong> to <code>10<sup>8</sup></code>.</p>
13+
14+
<p>Two values <code>x</code> and <code>y</code> are <strong>non-coprime</strong> if <code>GCD(x, y) &gt; 1</code> where <code>GCD(x, y)</code> is the <strong>Greatest Common Divisor</strong> of <code>x</code> and <code>y</code>.</p>
15+
16+
<p>&nbsp;</p>
17+
<p><strong class="example">Example 1:</strong></p>
18+
19+
<pre>
20+
<strong>Input:</strong> nums = [6,4,3,2,7,6,2]
21+
<strong>Output:</strong> [12,7,6]
22+
<strong>Explanation:</strong>
23+
- (6, 4) are non-coprime with LCM(6, 4) = 12. Now, nums = [<strong><u>12</u></strong>,3,2,7,6,2].
24+
- (12, 3) are non-coprime with LCM(12, 3) = 12. Now, nums = [<strong><u>12</u></strong>,2,7,6,2].
25+
- (12, 2) are non-coprime with LCM(12, 2) = 12. Now, nums = [<strong><u>12</u></strong>,7,6,2].
26+
- (6, 2) are non-coprime with LCM(6, 2) = 6. Now, nums = [12,7,<u><strong>6</strong></u>].
27+
There are no more adjacent non-coprime numbers in nums.
28+
Thus, the final modified array is [12,7,6].
29+
Note that there are other ways to obtain the same resultant array.
30+
</pre>
31+
32+
<p><strong class="example">Example 2:</strong></p>
33+
34+
<pre>
35+
<strong>Input:</strong> nums = [2,2,1,1,3,3,3]
36+
<strong>Output:</strong> [2,1,1,3]
37+
<strong>Explanation:</strong>
38+
- (3, 3) are non-coprime with LCM(3, 3) = 3. Now, nums = [2,2,1,1,<u><strong>3</strong></u>,3].
39+
- (3, 3) are non-coprime with LCM(3, 3) = 3. Now, nums = [2,2,1,1,<u><strong>3</strong></u>].
40+
- (2, 2) are non-coprime with LCM(2, 2) = 2. Now, nums = [<u><strong>2</strong></u>,1,1,3].
41+
There are no more adjacent non-coprime numbers in nums.
42+
Thus, the final modified array is [2,1,1,3].
43+
Note that there are other ways to obtain the same resultant array.
44+
</pre>
45+
46+
<p>&nbsp;</p>
47+
<p><strong>Constraints:</strong></p>
48+
49+
<ul>
50+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
51+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
52+
<li>The test cases are generated such that the values in the final array are <strong>less than or equal</strong> to <code>10<sup>8</sup></code>.</li>
53+
</ul>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def replaceNonCoprimes(self, nums: List[int]) -> List[int]:
3+
stk = []
4+
for num in nums:
5+
stk.append(num)
6+
while len(stk) > 1 and gcd(stk[-1], stk[-2]) > 1:
7+
stk.append(lcm(stk.pop(), stk.pop()))
8+
return stk

0 commit comments

Comments
 (0)