-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsum.py
More file actions
68 lines (60 loc) · 1.82 KB
/
sum.py
File metadata and controls
68 lines (60 loc) · 1.82 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class Solution(object):
def judgeSquareSum(self, c):
"""
:type c: int
:rtype: bool
"""
"""
# Brute force logic - Time Limit Exceeded
for i in range(c+1):
for j in range(i,c+1):
#print i,j
if i**2 + j**2 == c:
return True
return False
"""
"""
# Memory Issues
for i in range(c+1):
if i**2 <= c:
for j in range(i,c+1):
if j**2 <= c:
if i**2 + j**2 == c:
return True
return False
"""
"""
# Time Limit Exceeded
# Optimization - check how many square numbers exist within the range and then try adding them
# using math and square root
import math
score = int(math.sqrt(c))
perfect = []
for i in range(score+1):
# This condition if i**2 < c is satisfied by taking square root
p = i**2
if c-p in perfect:
return True
else:
perfect.append(p)
for n in perfect:
if c-n in perfect:
return True
return False
"""
# Nice logic - Optimization using while loop -
# * Decide on using while loop for more control -- practice
# * think with respect to operations from a boundary values
import math
score = int(math.sqrt(c))
i = 0
j = score
while i <= j:
perfect = i**2 + j**2
if perfect == c:
return True
if perfect > c:
j -= 1
else:
i += 1
return False