-
Notifications
You must be signed in to change notification settings - Fork 13
/
solution.js
27 lines (24 loc) · 1.1 KB
/
solution.js
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
/**
* @param {number} n - The target number for which we want to determine the minimum number of operations.
* @return {number} - The minimum number of operations needed to achieve the target number.
*/
var minSteps = function (n) {
// Initialize a variable to keep track of the total number of operations required.
let operations = 0;
// Start a loop from 2 up to and including the target number `n`.
// We start from 2 because 1 is not a valid factor for division in this context.
for (let i = 2; i <= n; i++) {
// As long as `n` is divisible by `i`, we can perform operations.
// This is equivalent to "pasting" the previously copied sequence.
while (n % i === 0) {
// Add the current divisor `i` to the operation count.
// This represents the series of "Copy All" and "Paste" operations required.
operations += i;
// Update `n` by dividing it by `i`.
// This reduces `n` by removing the effect of the last "Paste" operation.
n /= i;
}
}
// Return the total number of operations required to construct the string of length `n`.
return operations;
};