MediumNo. 1558
Minimum Numbers of Function Calls to Make Target Array — LeetCode 1558 Python Solution
- Pattern
- Bit Manipulation
- Topics
- Greedy, Bit Manipulation, Array
- Reading time
- 2 min
- Source
- leetcode.com
The problem
You are given an integer array nums. You have an integer array arr of the same length with all values set to 0 initially.
Example
- Input
- nums = [1,5]
- Output
- 5
- Explanation
- Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Python solution
class Solution:
def minOperations(self, nums: List[int]) -> int:
return sum(v.bit_count() for v in nums) + max(0, max(nums).bit_length() - 1)Complexity
- Time
- O(n log n)
- Space · auxiliary
- O(1) to O(n)
Pattern: Bit Manipulation
Use XOR, masks and the low-bit trick to replace whole data structures with an integer. LeetCode 1558. Minimum Numbers of Function Calls to Make Target Array is filed here because LeetCode tags it Bit Manipulation, which is the vocabulary this hub collects.
The bit manipulation guide has the Python template for the pattern and the 194 LeetCode problems that use it.
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0861 | Score After Flipping Matrix | Medium |
| 2680 | Maximum OR | Medium |
| 2835 | Minimum Operations to Form Subsequence With Target Sum | Hard |
| 2871 | Split Array Into Maximum Number of Subarrays | Medium |
| 1561 | Maximum Number of Coins You Can Get | Medium |
| 0011 | Container With Most Water | Medium |
Frequently asked questions
- How hard is LeetCode 1558. Minimum Numbers of Function Calls to Make Target Array?
LeetCode 1558. Minimum Numbers of Function Calls to Make Target Array is rated Medium on LeetCode.
- What topics does LeetCode 1558. Minimum Numbers of Function Calls to Make Target Array cover?
LeetCode 1558. Minimum Numbers of Function Calls to Make Target Array is tagged Greedy, Bit Manipulation and Array on LeetCode.