Skip to content

Commit e891717

Browse files
authored
Create shortest-distance-to-target-string-in-a-circular-array.py
1 parent 9c84f19 commit e891717

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
# array
5+
class Solution(object):
6+
def closetTarget(self, words, target, startIndex):
7+
"""
8+
:type words: List[str]
9+
:type target: str
10+
:type startIndex: int
11+
:rtype: int
12+
"""
13+
INF = float("inf")
14+
result = INF
15+
for i, w in enumerate(words):
16+
if w == target:
17+
result = min(result, (i-startIndex)%len(words), (startIndex-i)%len(words))
18+
return result if result != INF else -1

0 commit comments

Comments
 (0)