-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2654.cpp
More file actions
25 lines (24 loc) · 697 Bytes
/
2654.cpp
File metadata and controls
25 lines (24 loc) · 697 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
25
class Solution {
public:
int minOperations(vector<int>& nums) {
int n = nums.size();
int countOnes = 0;
for (auto num : nums) {
if (num == 1) countOnes++;
}
if (countOnes > 0) return n - countOnes;
int minLength = INT_MAX;
for (int i = 0; i < n; ++i) {
int g = nums[i];
for (int j = i + 1; j < n; ++j) {
g = __gcd(g, nums[j]);
if (g == 1) {
minLength = min(minLength, j - i + 1);
break;
}
}
}
if (minLength == INT_MAX) return -1;
return minLength - 1 + n - 1;
}
};