Minimum Numbers of Function Calls to Make Target Array
You are given an integer array nums. You have an integer array arr of the same length with all values set to 0 initially. You also have the following modify function:
- Increment any single element of
arrby1. - Double all elements of
arr.
You want to use the modify function to convert arr to nums using the minimum number of calls.
Return the minimum number of function calls to make nums from arr.
The test cases are generated so that the answer fits in a 32-bit signed integer.
Example 1
Input
nums = [1,5]Output
5The target can be reached with 1 increment on the second element, 2 double operations, and 2 more increments, for a total of 5 operations.
Example 2
Input
nums = [2,2]Output
3Incrementing both elements once takes 2 operations, then doubling all elements once reaches [2, 2], for a total of 3 operations.
Constraints
- 1 <= nums.length <= 10^5
- 0 <= nums[i] <= 10^9