Skip to content

Commit 20aa9de

Browse files
committed
2024-05-07 v. 5.6.2: added "2379. Minimum Recolors to Get K Consecutive Black Blocks"
1 parent 30e6af9 commit 20aa9de

4 files changed

+33
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,3 +422,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
422422
| 2363. Merge Similar Items | [Link](https://leetcode.com/problems/merge-similar-items/) | [Link](./lib/easy/2363_merge_similar_items.rb) |
423423
| 2367. Number of Arithmetic Triplets | [Link](https://leetcode.com/problems/number-of-arithmetic-triplets/) | [Link](./lib/easy/2367_number_of_arithmetic_triplets.rb) |
424424
| 2373. Largest Local Values in a Matrix | [Link](https://leetcode.com/problems/largest-local-values-in-a-matrix/) | [Link](./lib/easy/2373_largest_local_values_in_a_matrix.rb) |
425+
| 2379. Minimum Recolors to Get K Consecutive Black Blocks | [Link](https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/) | [Link](./lib/easy/2379_minimum_recolors_to_get_k_consecutive_black_blocks.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.6.1'
8+
s.version = '5.6.2'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
1111
s.executable = 'leetcode-ruby'
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/
4+
# @param {String} blocks
5+
# @param {Integer} k
6+
# @return {Integer}
7+
def minimum_recolors(blocks, k)
8+
n = blocks.length
9+
result = n
10+
curr = 0
11+
(0...n).each do |i|
12+
curr += 1 if blocks[i] == 'B'
13+
curr -= 1 if i >= k && blocks[i - k] == 'B'
14+
15+
result = [result, k - curr].min
16+
end
17+
18+
result
19+
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/2379_minimum_recolors_to_get_k_consecutive_black_blocks'
5+
require 'minitest/autorun'
6+
7+
class MinimumRecolorsToGetKConsecutiveBlackBlocksTest < ::Minitest::Test
8+
def test_default
9+
assert_equal(3, minimum_recolors('WBBWWBBWBW', 7))
10+
assert_equal(0, minimum_recolors('WBWBBBW', 2))
11+
end
12+
end

0 commit comments

Comments
 (0)