2025-08-15 13:25:44 +0000 UTC

Minimum Distance to the Target Element

Code

class Solution:
    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
        length = len(nums)
        dist = 0
        while dist < length:
            left, right = start - dist, start + dist
            if left >= 0 and nums[left] == target:
                return dist
            if right < length and nums[right] == target:
                return dist
            dist += 1
        raise Exception