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.
deforganizingContainers(container)->str:'''returnsImpossible|Possibleforwhethertheballscanbesplit'''# this problem is very easy with two containers# the count of balls in one container must be equal to the counts of one type# this logic can be extended to the fact that for every count of a ball type there needs to be a container that fits it# in other words the sum of the columns must be equal to the sum of some other row (and that row cannot already be filled with a different ball type)# if you are wondering why zip(*container) gives a list of a ball type's count in each container here is the breakdown# container = [# [1,4],# [2,3],# ]# the * operator unpacks the container matrix # zip(*container) = zip([1,4], [2,3])## zip then joins equal indices together# list(zip(*container)) = [(1,2), (4,3)]sums_of_balls=[sum(b)forbinzip(*container)]sums_of_containers=[sum(c)forcincontainer]sums_of_balls.sort()sums_of_containers.sort()return"Possible"ifsums_of_balls==sums_of_containerselse"Impossible"
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Organizing Containers of Balls
You are viewing a single comment's thread. Return to all comments →