Minimum Replacements to Sort the Array LeetCode solution in C++

1
class Solution {
2
public:
3
long long minimumReplacement(vector<int> &nums) {
4
long long res = 0;
5
int n = nums.size();
6
int mxm = nums[n - 1];
7
long long val;
8
for (int i = n - 2; i >= 0; i--) {
9
// minimum no. of elemetns nums[i] is divided such that every number is
10
// less than mxm and minimum is maximized
11
val = ceil(nums[i] / (double)mxm);
12

13
// no. of steps is val-1
14
res += (val - 1);
15

16
// the new maximized minimum value
17
val = nums[i] / val;
18
mxm = val;
19
}
20
return res;
21
}
22
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0