We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
fromsysimportstdinif__name__=='__main__':(N,M)=[int(i)foriinstdin.readline().split()]vals=[0]*Nforiinrange(M):(a,b,k)=[int(i)foriinstdin.readline().split()]vals[a-1]+=ktry:vals[b]-=kexcept:# out of range of list (at the end)passprev=0foriinrange(len(vals)):prev+=vals[i]vals[i]=prevprint(max(vals))
Small difference is that the array is not N+1 long, but handle out of range via catching the exception.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Array Manipulation
You are viewing a single comment's thread. Return to all comments →
Same algorithm in python3
Small difference is that the array is not N+1 long, but handle out of range via catching the exception.