-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathAC_BFS_nsqrtn.py
37 lines (32 loc) · 967 Bytes
/
AC_BFS_nsqrtn.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Author: illuz <iilluzen[at]gmail.com>
# File: AC_BFS_nsqrtn.py
# Create Date: 2015-10-26 13:25:49
import math
class Solution(object):
def numSquares(self, n):
"""
:type n: int
:rtype: int
"""
square = [s**2 for s in xrange(1, int(math.sqrt(n))+1)]
to_check = set()
to_check.add(n)
# to_check = [n] 1. MLE
cnt = 0
while 0 not in to_check:
cnt += 1
# to_check = [x - y for x in to_check for y in square if x >= y] 1. MLE
# to_check = set([x - y for x in to_check for y in square if x >= y]) 2. TLE
tmp = set()
for x in to_check:
for y in square:
if x < y:
break
tmp.add(x - y)
to_check = tmp
return cnt
s = Solution()
print s.numSquares(12)
print s.numSquares(59)