From 45faf83d373339a7ec8fcad804e9323d464a97fc Mon Sep 17 00:00:00 2001 From: wolves Date: Wed, 15 Apr 2026 00:51:20 +0800 Subject: [PATCH] routine --- 26/04/2515.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 26/04/2515.go diff --git a/26/04/2515.go b/26/04/2515.go new file mode 100644 index 0000000..0a93787 --- /dev/null +++ b/26/04/2515.go @@ -0,0 +1,25 @@ +package leetcode + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} + +func closestTarget(words []string, target string, startIndex int) int { + ans := len(words) + n := len(words) + + for i, word := range words { + if word == target { + dist := abs(i - startIndex) + ans = min(ans, min(dist, n-dist)) + } + } + + if ans < n { + return ans + } + return -1 +}