blob: 7d87d805a48b6c68c32f539e5a1dc7036b082e84 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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))
|