diff --git a/README.md b/README.md index 8fccd642..13289ba3 100644 --- a/README.md +++ b/README.md @@ -446,3 +446,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 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) | | 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) | | 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) | +| 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) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 38dea510..dd9c00d7 100644 --- a/leetcode-ruby.gemspec +++ b/leetcode-ruby.gemspec @@ -5,7 +5,7 @@ require 'English' ::Gem::Specification.new do |s| s.required_ruby_version = '>= 3.0' s.name = 'leetcode-ruby' - s.version = '5.8.5' + s.version = '5.8.6' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE] s.executable = 'leetcode-ruby' diff --git a/lib/easy/2515_shortest_distance_to_target_string_in_a_circular_array.rb b/lib/easy/2515_shortest_distance_to_target_string_in_a_circular_array.rb new file mode 100644 index 00000000..4b135b43 --- /dev/null +++ b/lib/easy/2515_shortest_distance_to_target_string_in_a_circular_array.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/shortest-distance-to-target-string-in-a-circular-array/ +# @param {String[]} words +# @param {String} target +# @param {Integer} start_index +# @return {Integer} +def closet_target(words, target, start_index) + max = 1_000_000_000_000_000_000 + result = max + + words.each_with_index do |word, i| + next unless word == target + + l = (i - start_index).abs + result = [result, l, words.length - l].min + end + + result == max ? -1 : result +end diff --git a/test/easy/test_2515_shortest_distance_to_target_string_in_a_circular_array.rb b/test/easy/test_2515_shortest_distance_to_target_string_in_a_circular_array.rb new file mode 100644 index 00000000..b92c2207 --- /dev/null +++ b/test/easy/test_2515_shortest_distance_to_target_string_in_a_circular_array.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/easy/2515_shortest_distance_to_target_string_in_a_circular_array' +require 'minitest/autorun' + +class ShortestDistanceToTargetStringInACircularArrayTest < ::Minitest::Test + def test_default + assert_equal(1, closet_target(%w[hello i am leetcode hello], 'hello', 1)) + assert_equal(1, closet_target(%w[a b leetcode], 'leetcode', 0)) + assert_equal(-1, closet_target(%w[i eat leetcode], 'ate', 0)) + end +end