2022-03-27 20:45:09 +08:00
< p > You are given an integer array < code > nums< / code > . You have an integer array < code > arr< / code > of the same length with all values set to < code > 0< / code > initially. You also have the following < code > modify< / code > function:< / p >
< img alt = "" src = "https://assets.leetcode.com/uploads/2020/07/10/sample_2_1887.png" style = "width: 573px; height: 294px;" / >
2023-12-09 18:42:21 +08:00
< p > You want to use the modify function to convert < code > arr< / code > to < code > nums< / code > using the minimum number of calls.< / p >
2022-03-27 20:45:09 +08:00
< p > Return < em > the minimum number of function calls to make < / em > < code > nums< / code > < em > from < / em > < code > arr< / code > .< / p >
< p > The test cases are generated so that the answer fits in a < strong > 32-bit< / strong > signed integer.< / p >
< p > < / p >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 1:< / strong > < / p >
2022-03-27 20:45:09 +08:00
< pre >
< strong > Input:< / strong > nums = [1,5]
< strong > Output:< / strong > 5
< strong > Explanation:< / strong > Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> < strong > [1, 5]< / strong > (2 operations).
Total of operations: 1 + 2 + 2 = 5.
< / pre >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 2:< / strong > < / p >
2022-03-27 20:45:09 +08:00
< pre >
< strong > Input:< / strong > nums = [2,2]
< strong > Output:< / strong > 3
< strong > Explanation:< / strong > Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> < strong > [2, 2]< / strong > (1 operation).
Total of operations: 2 + 1 = 3.
< / pre >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 3:< / strong > < / p >
2022-03-27 20:45:09 +08:00
< pre >
< strong > Input:< / strong > nums = [4,2,5]
< strong > Output:< / strong > 6
< strong > Explanation:< / strong > (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> < strong > [4,2,5]< / strong > (nums).
< / pre >
< p > < / p >
< p > < strong > Constraints:< / strong > < / p >
< ul >
< li > < code > 1 < = nums.length < = 10< sup > 5< / sup > < / code > < / li >
< li > < code > 0 < = nums[i] < = 10< sup > 9< / sup > < / code > < / li >
< / ul >