We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
O(N) complecity (no extra "moves" on the begining)
EEEOEEEOEOEEEOEEE
look at that like chain EEE OEEEO E OEEEO EEE (Even, Odd)
we go from left to right
if E - just skip
if O - if this first , we have to bring it to next O
(because only two O's next each other can become EE)
so we count distance to next O , than multiply by 2
that's our loaves to eliminate those two OO (so called price)
and so on
if we ended up with still building chain (aka looking for next O) - means we have odd number of O's - no way to make them happy, print "NO"
loaves = 0
isBuilding = False
previousIndex = 0
for i in range(len(B)):
if B[i] % 2 == 1:
if isBuilding :
loaves += (i - previousIndex) * 2
isBuilding = False
else:
isBuilding = True
previousIndex = i
return "NO" if isBuilding else loaves
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Fair Rations
You are viewing a single comment's thread. Return to all comments →
Did in Python
O(N) complecity (no extra "moves" on the begining)
EEEOEEEOEOEEEOEEE look at that like chain EEE OEEEO E OEEEO EEE (Even, Odd) we go from left to right if E - just skip if O - if this first , we have to bring it to next O (because only two O's next each other can become EE) so we count distance to next O , than multiply by 2
that's our loaves to eliminate those two OO (so called price)
and so on
if we ended up with still building chain (aka looking for next O) - means we have odd number of O's - no way to make them happy, print "NO"