Skip to content

Commit

Permalink
Create squirrel-simulation.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Jun 6, 2017
1 parent d38ec4d commit 5f08cf7
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Python/squirrel-simulation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Time: O(n)
# Space: O(1)

class Solution(object):
def minDistance(self, height, width, tree, squirrel, nuts):
"""
:type height: int
:type width: int
:type tree: List[int]
:type squirrel: List[int]
:type nuts: List[List[int]]
:rtype: int
"""
def distance(a, b):
return abs(a[0] - b[0]) + abs(a[1] - b[1])

result = 0
d = float("inf")
for nut in nuts:
result += (distance(nut, tree) * 2)
d = min(d, distance(nut, squirrel) - distance(nut, tree))
return result + d

0 comments on commit 5f08cf7

Please sign in to comment.