Skip to content

Commit 5f08cf7

Browse files
authored
Create squirrel-simulation.py
1 parent d38ec4d commit 5f08cf7

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Python/squirrel-simulation.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def minDistance(self, height, width, tree, squirrel, nuts):
6+
"""
7+
:type height: int
8+
:type width: int
9+
:type tree: List[int]
10+
:type squirrel: List[int]
11+
:type nuts: List[List[int]]
12+
:rtype: int
13+
"""
14+
def distance(a, b):
15+
return abs(a[0] - b[0]) + abs(a[1] - b[1])
16+
17+
result = 0
18+
d = float("inf")
19+
for nut in nuts:
20+
result += (distance(nut, tree) * 2)
21+
d = min(d, distance(nut, squirrel) - distance(nut, tree))
22+
return result + d

0 commit comments

Comments
 (0)