You are given an array of integers nums. Perform the following steps:
1. Find any two adjacent numbers in nums that are non-coprime.
2. If no such numbers are found, stop the process.
3. Otherwise, delete the two numbers and replace them with their LCM (Least Common Multiple).
4. Repeat this process as long as you keep finding two adjacent non-coprime numbers.
5. Return the final modified array. It can be shown that replacing adjacent non-coprime numbers in any arbitrary order will lead to the same result.
The test cases are generated such that the values in the final array are less than or equal to 108.
Two values x and y are non-coprime if GCD(x, y) > 1 where GCD(x, y) is the Greatest Common Divisor of x and y.
배열을 다음 단계를 거쳐 가공하시오
해당 가공의 결과로 나올 배열을 리턴하시오
[6,4,3,2,7,6,2]
=> [12, 7, 6]
class Solution:
def replaceNonCoprimes(self, nums: List[int]) -> List[int]:
s = []
for num in nums:
if not s:
s.append(num)
continue
while s:
g = gcd(s[-1], num)
if g > 1:
v = s.pop()
num = v * num // g
else:
break
s.append(num)
return s