-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3326.cpp
More file actions
31 lines (31 loc) · 836 Bytes
/
3326.cpp
File metadata and controls
31 lines (31 loc) · 836 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
26
27
28
29
30
31
class Solution {
public:
unordered_map<int, int> memo;
int greatestNum(int num) {
if (memo.count(num)) return memo[num];
int res = -1;
for (int i = 2; i * i <= num; ++i) {
int a = i;
int b = num / a;
if (a * b == num) {
res = max(res, a);
res = max(res, b);
}
}
return memo[num] = res;
}
int minOperations(vector<int>& nums) {
int n = nums.size();
int res = 0;
for (int i = n - 2; i >= 0; --i) {
int next = nums[i + 1];
while (nums[i] > next) {
int divide = greatestNum(nums[i]);
if (divide == -1) return -1;
nums[i] /= divide;
res++;
}
}
return res;
}
};