-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay 37
More file actions
24 lines (22 loc) · 726 Bytes
/
Day 37
File metadata and controls
24 lines (22 loc) · 726 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#2654.minimum-number-of-operations-to-make-all-array-elements-equal-to-1
from math import gcd
from typing import List
class Solution:
def minOperations(self, nums: List[int]) -> int:
n = len(nums)
overall_gcd = nums[0]
for num in nums:
overall_gcd = gcd(overall_gcd, num)
if overall_gcd != 1:
return -1
if 1 in nums:
return n - nums.count(1)
min_len = float('inf')
for i in range(n):
g = nums[i]
for j in range(i + 1, n):
g = gcd(g, nums[j])
if g == 1:
min_len = min(min_len, j - i + 1)
break
return (min_len - 1) + (n - 1)