From 20aa9ded9dedf0420825df12439876b22a7a3dda Mon Sep 17 00:00:00 2001 From: fartem Date: Tue, 7 May 2024 06:17:22 +0300 Subject: [PATCH] 2024-05-07 v. 5.6.2: added "2379. Minimum Recolors to Get K Consecutive Black Blocks" --- README.md | 1 + leetcode-ruby.gemspec | 2 +- ...olors_to_get_k_consecutive_black_blocks.rb | 19 +++++++++++++++++++ ...olors_to_get_k_consecutive_black_blocks.rb | 12 ++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 lib/easy/2379_minimum_recolors_to_get_k_consecutive_black_blocks.rb create mode 100644 test/easy/test_2379_minimum_recolors_to_get_k_consecutive_black_blocks.rb diff --git a/README.md b/README.md index b11bf66a..a65bb483 100644 --- a/README.md +++ b/README.md @@ -422,3 +422,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 2363. Merge Similar Items | [Link](https://leetcode.com/problems/merge-similar-items/) | [Link](./lib/easy/2363_merge_similar_items.rb) | | 2367. Number of Arithmetic Triplets | [Link](https://leetcode.com/problems/number-of-arithmetic-triplets/) | [Link](./lib/easy/2367_number_of_arithmetic_triplets.rb) | | 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) | +| 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) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 93e1827e..95a62310 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.6.1' + s.version = '5.6.2' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE] s.executable = 'leetcode-ruby' diff --git a/lib/easy/2379_minimum_recolors_to_get_k_consecutive_black_blocks.rb b/lib/easy/2379_minimum_recolors_to_get_k_consecutive_black_blocks.rb new file mode 100644 index 00000000..987aa0e4 --- /dev/null +++ b/lib/easy/2379_minimum_recolors_to_get_k_consecutive_black_blocks.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/ +# @param {String} blocks +# @param {Integer} k +# @return {Integer} +def minimum_recolors(blocks, k) + n = blocks.length + result = n + curr = 0 + (0...n).each do |i| + curr += 1 if blocks[i] == 'B' + curr -= 1 if i >= k && blocks[i - k] == 'B' + + result = [result, k - curr].min + end + + result +end diff --git a/test/easy/test_2379_minimum_recolors_to_get_k_consecutive_black_blocks.rb b/test/easy/test_2379_minimum_recolors_to_get_k_consecutive_black_blocks.rb new file mode 100644 index 00000000..0b0c1d79 --- /dev/null +++ b/test/easy/test_2379_minimum_recolors_to_get_k_consecutive_black_blocks.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/easy/2379_minimum_recolors_to_get_k_consecutive_black_blocks' +require 'minitest/autorun' + +class MinimumRecolorsToGetKConsecutiveBlackBlocksTest < ::Minitest::Test + def test_default + assert_equal(3, minimum_recolors('WBBWWBBWBW', 7)) + assert_equal(0, minimum_recolors('WBWBBBW', 2)) + end +end