You are viewing a single comment's thread. Return to all comments →
def cookies(k, A): A.sort(reverse = True) queue = [] mixes = 0 while True: if len(A) <= 1 and queue: A = queue[::-1] + A queue = [] if not A and not queue: return mixes if queue: if A[-1] <= queue[0]: last_num = A.pop() else: last_num = queue.pop(0) else: last_num = A.pop() if last_num >= k: next elif len(A) == 0 and last_num < k: return -1 else: if not queue: sec_num = A.pop() else: if A[-1] <= queue[0]: sec_num = A.pop() else: sec_num = queue.pop(0) new_num = last_num + 2 * sec_num queue.append(new_num) mixes += 1
Seems like cookies are disabled on this browser, please enable them to open this website
Jesse and Cookies
You are viewing a single comment's thread. Return to all comments →
def cookies(k, A): A.sort(reverse = True) queue = [] mixes = 0 while True: if len(A) <= 1 and queue: A = queue[::-1] + A queue = [] if not A and not queue: return mixes if queue: if A[-1] <= queue[0]: last_num = A.pop() else: last_num = queue.pop(0) else: last_num = A.pop() if last_num >= k: next elif len(A) == 0 and last_num < k: return -1 else: if not queue: sec_num = A.pop() else: if A[-1] <= queue[0]: sec_num = A.pop() else: sec_num = queue.pop(0) new_num = last_num + 2 * sec_num queue.append(new_num) mixes += 1