Skip to content

Commit 78eff7c

Browse files
authored
Merge pull request #642 from fartem/2511_Maximum_Enemy_Forts_That_Can_Be_Captured
2024-06-10 v. 5.8.5: added "2511. Maximum Enemy Forts That Can Be Captured"
2 parents 94220f6 + fa8dae9 commit 78eff7c

4 files changed

+35
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,3 +445,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
445445
| 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) |
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) |
448+
| 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) |

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.4'
8+
s.version = '5.8.5'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
1111
s.executable = 'leetcode-ruby'
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/maximum-enemy-forts-that-can-be-captured/
4+
# @param {Integer[]} forts
5+
# @return {Integer}
6+
def capture_forts(forts)
7+
result = 0
8+
i = 0
9+
j = 0
10+
while i < forts.length
11+
unless forts[i].zero?
12+
result = [result, i - j - 1].max if forts[j] == -forts[i]
13+
14+
j = i
15+
end
16+
17+
i += 1
18+
end
19+
20+
result
21+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/easy/2511_maximum_enemy_forts_that_can_be_captured'
5+
require 'minitest/autorun'
6+
7+
class MaximumEnemyFortsThatCanBeCapturedTest < ::Minitest::Test
8+
def test_default
9+
assert_equal(4, capture_forts([1, 0, 0, -1, 0, 0, 0, 0, 1]))
10+
assert_equal(0, capture_forts([0, 0, 1, -1]))
11+
end
12+
end

0 commit comments

Comments
 (0)