Skip to content

Instantly share code, notes, and snippets.

@jakab922
Created December 5, 2014 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakab922/2705f4783768ffd94a06 to your computer and use it in GitHub Desktop.
Save jakab922/2705f4783768ffd94a06 to your computer and use it in GitHub Desktop.
def check(A, B, C, index):
totals = [0 for _ in xrange(index)]
for i in xrange(index - 1, -1, -1):
totals[i] += B[i]
if C[i] != -1:
totals[C[i]] += totals[i]
return all([totals[i] <= A[i] for i in xrange(index)])
def solution(A, B, C):
n = len(A)
if n == 0:
return 0
low, high = 0, n
while high - low > 1:
mid = (low + high) / 2
if check(A, B, C, mid):
low = mid
else:
high = mid
if check(A, B, C, high):
return high
else:
return low
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment