finish extra/ion-flux-relabeling #16
@ -79,3 +79,6 @@ CFcSBwgCCBoHRhkKU1cGAA4AGU5YQR5THBwNFwoGGAxTQQMQVBUSBg4EAAwQRhUQVBUHFAQTGRpT QQM
|
|||||||
|
|
||||||
### queue-to-do
|
### queue-to-do
|
||||||
* Completed in: 9 hrs, 13 mins, 42 secs.
|
* Completed in: 9 hrs, 13 mins, 42 secs.
|
||||||
|
|
||||||
|
### ion-flux-relabeling
|
||||||
|
* Completed in: 9 hrs, 33 mins, 36 secs.
|
||||||
|
|||||||
28
extra/ion-flux-relabeling/solution.py
Normal file
28
extra/ion-flux-relabeling/solution.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
def solution(h, q):
|
||||||
|
return [converter(h, n) for n in q]
|
||||||
|
|
||||||
|
def converter(h, n):
|
||||||
|
if n == 2**h - 1:
|
||||||
|
return -1
|
||||||
|
return reduce(n)
|
||||||
|
|
||||||
|
def reduce(n):
|
||||||
|
b = bin(n)[2:]
|
||||||
|
l = len(b)
|
||||||
|
c = b.count('1')
|
||||||
|
if l == c: # 2**l - 1
|
||||||
|
return 2**(l+1) - 1
|
||||||
|
fold = 2**(l-1) - 1
|
||||||
|
m = n - fold
|
||||||
|
if m == fold: # 2 * (2**(l-1) - 1)
|
||||||
|
return reduce(m)
|
||||||
|
return reduce(m) + fold
|
||||||
|
|
||||||
|
tests = [
|
||||||
|
([3, [7, 3, 5, 1]], [-1, 7, 6, 3]),
|
||||||
|
([5, [19, 14, 28]], [21, 15, 29]),
|
||||||
|
]
|
||||||
|
|
||||||
|
for i, o in tests:
|
||||||
|
result = solution(*i)
|
||||||
|
print (i, result == o, result, o)
|
||||||
Loading…
x
Reference in New Issue
Block a user