commit 35bc523af533220c03d188d6f5c1f8ed1439592c
parent ffcb1c466effc25618faa496e1acec67487e601a
Author: Jack Sangdahl <jack@pngu.org>
Date: Fri, 9 Jan 2026 20:51:14 -0700
1558. minimum numbers of function calls to make target array
Diffstat:
2 files changed, 58 insertions(+), 0 deletions(-)
diff --git a/min_num_func_call_arr.c b/min_num_func_call_arr.c
@@ -0,0 +1,32 @@
+// 1558. Minimum Numbers of Function Calls to Make Target Array
+#include "leetcode.h"
+
+/*
+ * 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. you want to use the modify function to covnert '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 such
+ * that the answer fits in a 32 bit signed integer.
+ */
+
+int minOperations(int *nums, int numsSize) {
+ int ans = 0, max = 1;
+ for (int i = 0; i < numsSize; i++) {
+ int bits = 0;
+ while (nums[i] > 0) {
+ ans += nums[i] & 1;
+ bits++;
+ nums[i] >>= 1;
+ }
+ max = fmax(max, bits);
+ }
+ return ans + max - 1;
+}
+
+int main() {
+ int n1[] = {1, 5}, n2[] = {2, 2}, n3[] = {4, 2, 5};
+ printf("%d\n", minOperations(n1, ARRAY_SIZE(n1))); // expect: 5
+ printf("%d\n", minOperations(n2, ARRAY_SIZE(n2))); // expect: 3
+ printf("%d\n", minOperations(n3, ARRAY_SIZE(n3))); // expect: 6
+}
diff --git a/min_num_func_call_arr.py b/min_num_func_call_arr.py
@@ -0,0 +1,26 @@
+# 1558. 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. you want to use the modify function to covnert '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 such
+that the answer fits in a 32 bit signed integer.
+"""
+
+
+class Solution(object):
+ def minOperations(self, nums):
+ """
+ :type nums: List[int]
+ :rtype: int
+ """
+ return sum(bin(i).count("1") for i in nums) + len(bin(max(nums))) - 3
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.minOperations(nums=[1, 5]))
+ print(obj.minOperations(nums=[2, 2]))
+ print(obj.minOperations(nums=[4, 2, 5]))