2018-11-07 leetcode Perfect Squares Perfect SquaresBinary Search Version - 356ms 12345678910111213141516171819import mathclass Solution: def numSquares(self, n): """ :type n: int :rtype: int """ s = [i*i for i in range(1,int(math.sqrt(n))+1)] # Square numbers <= n l = 0 # BFS level currentLevel = [0] # List of numbers in BFS level l while True: nextLevel = [] for a in currentLevel: for b in s: if a+b == n: return l+1 # Found n if a+b < n: nextLevel.append(a+b) currentLevel = list(set(nextLevel)) # Remove duplicates l += 1 test code 12In [315]: s = Solution(); t = s.numSquares(11); print(t)3 leet code Newer Binary Tree Zigzag Level Order Traversal Older Increasing Triplet Subsequence