Skip to content

Latest commit

 

History

History

minimum-distance-to-the-target-element

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Minimum distance to the target element

Problem link

Solutions

Solution.cpp

// https://leetcode.com/problems/minimum-distance-to-the-target-element

class Solution {
 public:
  int getMinDistance(vector<int>& nums, int target, int start) {
    int n = nums.size(), best = n;
    for (int i = 0; i < n; ++i)
      if (nums[i] == target) best = min(best, abs(start - i));
    return best;
  }
};

Tags