Skip to content

Latest commit

 

History

History

shortest-distance-to-target-string-in-a-circular-array

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Shortest distance to target string in a circular array

Problem link

Solutions

Solution.cpp

// https://leetcode.com/problems/shortest-distance-to-target-string-in-a-circular-array/

class Solution {
 public:
  int closetTarget(vector<string>& words, string target, int start) {
    int n = words.size(), ret = n;
    for (int i = 0; i < n; ++i) {
      if (words[i] != target) continue;
      int cur = abs(i - start);
      ret = min(ret, min(cur, n - cur));
    }
    return ret == n ? -1 : ret;
  }
};

Tags