You are given a positive integer k. Initially, you have an array nums = [1].
You can perform any of the following operations on the array any number of times (possibly zero):
-
Choose any element in the array and increase its value by
1
.
-
Duplicate any element in the array and add it to the end of the array.
Return the minimum number of operations required to make the sum of elements of the final array greater than or equal to k.
Function Signature
def min_operations(k: int) -> int:
...
Rules
-
Each increase and each duplication counts as one operation.
-
A duplicate copies the element's current value at the moment of duplication.
-
Operations can be applied in any order and the array can grow to any length.
Constraints
Examples
Input: k = 11
Output: 5
Increase the element to 4 (3 operations), then duplicate it twice (2 operations) to get [4, 4, 4] with sum 12. Increasing to 3 and duplicating three times to get [3, 3, 3, 3] also costs 5, and no plan with 4 operations reaches 11.
Input: k = 1
Output: 0
The initial array [1] already has sum 1, so no operation is needed.
Input: k = 100
Output: 18
Increase the element to 10 (9 operations) and duplicate it 9 times to get ten 10s, a sum of 100.