Created
August 18, 2015 10:28
-
-
Save isaiah-perumalla/5f5b1587acb4fbcb20cf to your computer and use it in GitHub Desktop.
solution to codility challenge https://codility.com/programmers/challenges/magnesium2014
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 solution(N, A, B, C): | |
edges = [(C[i],A[i],B[i]) for i in xrange(len(A))] | |
edges.sort(key=lambda e: e[0]) | |
maxLenAt = [0]*N | |
costAt = [0]*N | |
def updatePath(u,costu,lenu, v,costv, lenv, cost): | |
if costu < cost and lenu+1 > lenv: | |
maxLenAt[v] = lenu+1 | |
costAt[v] = cost | |
elif costu == cost and lenv == 0: | |
maxLenAt[v] = 1 | |
costAt[v] = cost | |
for cost,u,v in edges: | |
cost_u,cost_v = costAt[u], costAt[v] | |
len_u, len_v = maxLenAt[u], maxLenAt[v] | |
updatePath(u,cost_u,len_u, v, cost_v, len_v,cost) | |
updatePath(v, cost_v, len_v, u,cost_u,len_u,cost) | |
return max(maxLenAt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment