forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount-lattice-points-inside-a-circle.py
More file actions
36 lines (33 loc) · 1004 Bytes
/
count-lattice-points-inside-a-circle.py
File metadata and controls
36 lines (33 loc) · 1004 Bytes
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
# Time: O(n * r^2)
# Space: O(min(n * r^2, max_x * max_y))
# math, hash table
class Solution(object):
def countLatticePoints(self, circles):
"""
:type circles: List[List[int]]
:rtype: int
"""
lookup = set()
for x, y, r in circles:
for i in xrange(-r, r+1):
for j in xrange(-r, r+1):
if i**2+j**2 <= r**2:
lookup.add(((x+i), (y+j)))
return len(lookup)
# Time: O(n * max_x * max_y)
# Space: O(1)
# math
class Solution2(object):
def countLatticePoints(self, circles):
"""
:type circles: List[List[int]]
:rtype: int
"""
max_x = max(x+r for x, _, r in circles)
max_y = max(y+r for _, y, r in circles)
result = 0
for i in xrange(max_x+1):
for j in xrange(max_y+1):
if any((i-x)**2+(j-y)**2 <= r**2 for x, y, r in circles):
result += 1
return result