Skip to content

Commit 5c5b04f

Browse files
committed
1558. Minimum Numbers of Function Calls to Make Target Array: AC
1 parent b846f1b commit 5c5b04f

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1174,3 +1174,4 @@ mod s1552_magnetic_force_between_two_balls;
11741174
mod s1553_minimum_number_of_days_to_eat_n_oranges;
11751175
mod s1556_thousand_separator;
11761176
mod s1557_minimum_number_of_vertices_to_reach_all_nodes;
1177+
mod s1558_minimum_numbers_of_function_calls_to_make_target_array;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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 convert 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+
*/
40+
pub 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+
47+
impl Solution {
48+
pub fn min_operations(nums: Vec<i32>) -> i32 {
49+
let mut result = 0;
50+
let mut max = 0;
51+
for &num in nums.iter() {
52+
let mut cv = num;
53+
let mut temp = 0;
54+
while cv > 0 {
55+
if cv % 2 == 0 {
56+
cv /= 2;
57+
temp += 1;
58+
} else {
59+
cv -= 1;
60+
result += 1;
61+
}
62+
}
63+
max = max.max(temp);
64+
}
65+
66+
result + max
67+
}
68+
}
69+
70+
// submission codes end
71+
72+
#[cfg(test)]
73+
mod tests {
74+
use super::*;
75+
76+
#[test]
77+
fn test_1558_example_1() {
78+
let nums = vec![1, 5];
79+
80+
let result = 5;
81+
82+
assert_eq!(Solution::min_operations(nums), result);
83+
}
84+
85+
#[test]
86+
fn test_1558_example_2() {
87+
let nums = vec![2, 2];
88+
89+
let result = 3;
90+
91+
assert_eq!(Solution::min_operations(nums), result);
92+
}
93+
94+
#[test]
95+
fn test_1558_example_3() {
96+
let nums = vec![4, 2, 5];
97+
98+
let result = 6;
99+
100+
assert_eq!(Solution::min_operations(nums), result);
101+
}
102+
}

0 commit comments

Comments
 (0)