Created
December 5, 2014 11:19
-
-
Save jakab922/2705f4783768ffd94a06 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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