diff options
Diffstat (limited to 'perfect_squares.py')
-rw-r--r-- | perfect_squares.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/perfect_squares.py b/perfect_squares.py new file mode 100644 index 0000000..7d87d80 --- /dev/null +++ b/perfect_squares.py @@ -0,0 +1,43 @@ +# 279. Perfect Squares + +""" +given an integer 'n', return the least number of perfect square numbers that +sum to 'n'. a perfect square is an integer that is the square of an integer; +in other words, it is the product of some integer with itself. for example, +1,4,9,16 are perfect squares while 3 and 11 are not. +""" + + +class Solution(object): + def numSquares(self, n): + """ + :type n: int + :rtype: int + """ + if n < 2: + return n + lst = [] + i = 1 + while i * i <= n: + lst.append(i * i) + i += 1 + cnt = 0 + to_check = {n} + while to_check: + cnt += 1 + tmp = set() + for x in to_check: + for y in lst: + if x == y: + return cnt + if x < y: + break + tmp.add(x - y) + to_check = tmp + return cnt + + +if __name__ == "__main__": + obj = Solution() + print(obj.numSquares(12)) + print(obj.numSquares(13)) |