Minimum Numbers of Function Calls to Make Target Array LeetCode solution in Python

1
# Observe that:
2
# +1 places a 1 to the end of the int's binary representation
3
# (assuming a 0 there previously)
4
# x2 is a bitshift left
5
# So you basically just need to count all the ones in the binary representations
6
# and find how many shifts are required (largest bit length minus one).
7

8

9
class Solution:
10
def minOperations(self, nums: List[int]) -> int:
11
if max(nums) == 0:
12
return 0
13

14
return (
15
sum([x.bit_count() for x in nums]) + max([x.bit_length() for x in nums]) - 1
16
)

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0