Skip to content

Commit 6839f9d

Browse files
committedJun 11, 2024·
2024-06-11 v. 5.8.6: added "2515. Shortest Distance to Target String in a Circular Array"
1 parent 78eff7c commit 6839f9d

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,3 +446,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
446446
| 2500. Delete Greatest Value in Each Row | [Link](https://leetcode.com/problems/delete-greatest-value-in-each-row/) | [Link](./lib/easy/2500_delete_greatest_value_in_each_row.rb) |
447447
| 2506. Count Pairs Of Similar Strings | [Link](https://leetcode.com/problems/count-pairs-of-similar-strings/) | [Link](./lib/easy/2506_count_pairs_of_similar_strings.rb) |
448448
| 2511. Maximum Enemy Forts That Can Be Captured | [Link](https://leetcode.com/problems/maximum-enemy-forts-that-can-be-captured/) | [Link](./lib/easy/2511_maximum_enemy_forts_that_can_be_captured.rb) |
449+
| 2515. Shortest Distance to Target String in a Circular Array | [Link](https://leetcode.com/problems/shortest-distance-to-target-string-in-a-circular-array/) | [Link](./lib/easy/2515_shortest_distance_to_target_string_in_a_circular_array.rb) |

‎leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '5.8.5'
8+
s.version = '5.8.6'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
1111
s.executable = 'leetcode-ruby'
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/shortest-distance-to-target-string-in-a-circular-array/
4+
# @param {String[]} words
5+
# @param {String} target
6+
# @param {Integer} start_index
7+
# @return {Integer}
8+
def closet_target(words, target, start_index)
9+
max = 1_000_000_000_000_000_000
10+
result = max
11+
12+
words.each_with_index do |word, i|
13+
next unless word == target
14+
15+
l = (i - start_index).abs
16+
result = [result, l, words.length - l].min
17+
end
18+
19+
result == max ? -1 : result
20+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/easy/2515_shortest_distance_to_target_string_in_a_circular_array'
5+
require 'minitest/autorun'
6+
7+
class ShortestDistanceToTargetStringInACircularArrayTest < ::Minitest::Test
8+
def test_default
9+
assert_equal(1, closet_target(%w[hello i am leetcode hello], 'hello', 1))
10+
assert_equal(1, closet_target(%w[a b leetcode], 'leetcode', 0))
11+
assert_equal(-1, closet_target(%w[i eat leetcode], 'ate', 0))
12+
end
13+
end

0 commit comments

Comments
 (0)
Please sign in to comment.