Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions practice/leetcode/solutions_01800/solution_01848.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env python3

"""LeetCode solution 01848."""

import unittest


class Solution(object):
def getMinDistance(self, nums, target, start):
"""
:type nums: List[int]
:type target: int
:type start: int
:rtype: int
"""
n = len(nums)

for distance in range(n):
if start - distance >= 0 and nums[start - distance] == target:
return distance
if start + distance < n and distance != 0 and nums[start + distance] == target:
return distance

return -1


class TestSolution(unittest.TestCase):
def test_getMinDistance(self):
solution = Solution()
self.assertEqual(solution.getMinDistance([1, 2, 3, 4, 5], 5, 3), 1)
self.assertEqual(solution.getMinDistance([1], 1, 0), 0)


if __name__ == '__main__':
unittest.main()
Loading