From fa8dae98836341a7b76fc211d42f86edd5dfb47e Mon Sep 17 00:00:00 2001 From: fartem Date: Mon, 10 Jun 2024 10:02:27 +0300 Subject: [PATCH] 2024-06-10 v. 5.8.5: added "2511. Maximum Enemy Forts That Can Be Captured" --- README.md | 1 + leetcode-ruby.gemspec | 2 +- ...aximum_enemy_forts_that_can_be_captured.rb | 21 +++++++++++++++++++ ...aximum_enemy_forts_that_can_be_captured.rb | 12 +++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 lib/easy/2511_maximum_enemy_forts_that_can_be_captured.rb create mode 100644 test/easy/test_2511_maximum_enemy_forts_that_can_be_captured.rb diff --git a/README.md b/README.md index 68a0f5b3..8fccd642 100644 --- a/README.md +++ b/README.md @@ -445,3 +445,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 2496. Maximum Value of a String in an Array | [Link](https://leetcode.com/problems/maximum-value-of-a-string-in-an-array/) | [Link](./lib/easy/2496_maximum_value_of_a_string_in_an_array.rb) | | 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) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 33d959c5..38dea510 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.4' + s.version = '5.8.5' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE] s.executable = 'leetcode-ruby' diff --git a/lib/easy/2511_maximum_enemy_forts_that_can_be_captured.rb b/lib/easy/2511_maximum_enemy_forts_that_can_be_captured.rb new file mode 100644 index 00000000..06d9c6cc --- /dev/null +++ b/lib/easy/2511_maximum_enemy_forts_that_can_be_captured.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/maximum-enemy-forts-that-can-be-captured/ +# @param {Integer[]} forts +# @return {Integer} +def capture_forts(forts) + result = 0 + i = 0 + j = 0 + while i < forts.length + unless forts[i].zero? + result = [result, i - j - 1].max if forts[j] == -forts[i] + + j = i + end + + i += 1 + end + + result +end diff --git a/test/easy/test_2511_maximum_enemy_forts_that_can_be_captured.rb b/test/easy/test_2511_maximum_enemy_forts_that_can_be_captured.rb new file mode 100644 index 00000000..f265e11e --- /dev/null +++ b/test/easy/test_2511_maximum_enemy_forts_that_can_be_captured.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/easy/2511_maximum_enemy_forts_that_can_be_captured' +require 'minitest/autorun' + +class MaximumEnemyFortsThatCanBeCapturedTest < ::Minitest::Test + def test_default + assert_equal(4, capture_forts([1, 0, 0, -1, 0, 0, 0, 0, 1])) + assert_equal(0, capture_forts([0, 0, 1, -1])) + end +end