Last active
January 21, 2016 10:15
-
-
Save whatalnk/f0a11074efa7468c5549 to your computer and use it in GitHub Desktop.
TopCoder SRM #679 Div 2
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
from collections import deque | |
class ListeningSongs: | |
def listen(self, durations1, durations2, minutes, T): | |
if type(durations1) == int: | |
durations1 = deque([durations1]) | |
else: | |
durations1 = deque(sorted(list(durations1))) | |
if type(durations2) == int: | |
durations2 = deque([durations2]) | |
else: | |
durations2 = deque(sorted(list(durations2))) | |
seconds = minutes * 60 | |
listened = 0 | |
while listened < T * 2: | |
if (not durations1) or (not durations2): | |
return(-1) | |
songs = [durations1.popleft(), durations2.popleft()] | |
for s in songs: | |
listened += 1 | |
seconds -= s | |
if seconds < 0: | |
return(-1) | |
durations = deque(sorted(list(durations1) + list(durations2))) | |
while durations: | |
song = durations.popleft() | |
seconds -= song | |
listened += 1 | |
if seconds < 0: | |
return(listened - 1) | |
return(listened) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment