1558. Minimum Numbers of Function Calls to Make Target Array Medium

@problem@discussion
#Array#Greedy



1/**
2 * [1558] Minimum Numbers of Function Calls to Make Target Array
3 *
4 * 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:
5 * <img alt="" src="https://assets.leetcode.com/uploads/2020/07/10/sample_2_1887.png" style="width: 573px; height: 294px;" />
6 * You want to use the modify function to covert arr to nums using the minimum number of calls.
7 * Return the minimum number of function calls to make nums from arr.
8 * The test cases are generated so that the answer fits in a 32-bit signed integer.
9 *  
10 * Example 1:
11 * 
12 * Input: nums = [1,5]
13 * Output: 5
14 * Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
15 * Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
16 * Increment by 1 (both elements)  [0, 4] -> [1, 4] -> [1, 5] (2 operations).
17 * Total of operations: 1 + 2 + 2 = 5.
18 * 
19 * Example 2:
20 * 
21 * Input: nums = [2,2]
22 * Output: 3
23 * Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
24 * Double all the elements: [1, 1] -> [2, 2] (1 operation).
25 * Total of operations: 2 + 1 = 3.
26 * 
27 * Example 3:
28 * 
29 * Input: nums = [4,2,5]
30 * Output: 6
31 * Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
32 * 
33 *  
34 * Constraints:
35 * 
36 * 	1 <= nums.length <= 10^5
37 * 	0 <= nums[i] <= 10^9
38 * 
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/minimum-numbers-of-function-calls-to-make-target-array/
43// discuss: https://leetcode.com/problems/minimum-numbers-of-function-calls-to-make-target-array/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47impl Solution {
48    pub fn min_operations(nums: Vec<i32>) -> i32 {
49        0
50    }
51}
52
53// submission codes end
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn test_1558() {
61    }
62}
63


Back
© 2025 bowen.ge All Rights Reserved.