-
Notifications
You must be signed in to change notification settings - Fork 2
/
q1558.py
32 lines (29 loc) · 965 Bytes
/
q1558.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/python3
from typing import List
class Solution:
def minOperations(self, nums: List[int]) -> int:
result = 0
new_nums = sorted(nums, reverse=True)
while len(new_nums) > 0:
print(len(new_nums))
print(new_nums)
if new_nums[-1] == 0:
new_nums.pop(len(new_nums) - 1)
elif new_nums[-1] == 1:
new_nums.pop(len(new_nums) - 1)
result += 1
else:
flag = True
for i in range(len(new_nums)):
if new_nums[i] & 0x1 == 1:
new_nums[i] -= 1
result += 1
flag = False
if flag:
for i in range(len(new_nums)):
new_nums[i] >>= 1
result += 1
return result
nums = [2,4,8,16]
result = Solution().minOperations(nums)
print(result)