Skip to content

Instantly share code, notes, and snippets.

@pasindud
Last active February 19, 2021 11:01
Show Gist options
  • Save pasindud/10d23753f51ea9e192580bfd56294a44 to your computer and use it in GitHub Desktop.
Save pasindud/10d23753f51ea9e192580bfd56294a44 to your computer and use it in GitHub Desktop.
class Solution(object):
def getProbability(self, balls):
"""
:type balls: List[int]
:rtype: float
"""
trie = {}
newballs = balls + balls[0:-1]
perBall = []
stack = []
for x in range(0, len(balls)):
stack.append([[],len(perBall)])
perBall += [x+1 for _ in range(balls[x])]
newSeqList = []
# DFS and create set of balls in box 1 which are uniq.
# We do one box because, box 2 values can be induced by box 1
# Uniq in the sense that the order doesn't matter only the amount of ball matter
# [1,1,2] and [1,2,1] values in box 1 are considered same
# [1,1,2] and [1,1,1] values in box 1 are considered different
while stack:
theSeq, newPointer = stack.pop()
theSeq.append(perBall[newPointer])
if len(theSeq) == (len(perBall) /2):
newSeqList.append(theSeq)
continue
prev = None
for i in range(newPointer, len(perBall) - 1):
# Make new copy for the new seq
newSeq = theSeq[:]
if prev == None or prev != perBall[i + 1]:
stack.append([newSeq, i+1])
prev = perBall[i + 1]
for x in newSeqList:
print('- ' + str(x))
s = Solution()
balls = [1,2,1]
# balls = [3,2,1]
s.getProbability(balls)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment