Skip to content

Commit 859b4a6

Browse files
authored
Create minimum-distance-to-the-target-element.py
1 parent c84ad8a commit 859b4a6

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def getMinDistance(self, nums, target, start):
6+
"""
7+
:type nums: List[int]
8+
:type target: int
9+
:type start: int
10+
:rtype: int
11+
"""
12+
for i in xrange(len(nums)):
13+
if (start-i >= 0 and nums[start-i] == target) or \
14+
(start+i < len(nums) and nums[start+i] == target):
15+
break
16+
return i

0 commit comments

Comments
 (0)