Last active
December 31, 2015 14:29
SRM600 Div2 Medium bitのOR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ORSolitaireDiv2: | |
def getMinimum(self, numbers, goal): | |
numbers = list(numbers) | |
numbers.sort() | |
goal_bin = str(bin(goal))[2:] | |
goal_bin = list(reversed(goal_bin)) | |
counter = [0 for _ in goal_bin] | |
numbers = [num for num in numbers if num <= goal] | |
for num in numbers: | |
num_bin = list(reversed(str(bin(num))[2:])) | |
isInvalid = False | |
for i, n in enumerate(num_bin): | |
if goal_bin[i] == '0' and num_bin[i] == '1': | |
isInvalid = True | |
break | |
if isInvalid: | |
continue | |
for i, n in enumerate(num_bin): | |
if n == '1': | |
counter[i] += 1 | |
counter = [c for i, c in enumerate(counter) if goal_bin[i] == '1' ] | |
return min(counter) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment