Skip to content

Commit

Permalink
Minimum Recolors to Get K Consecutive Black Blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
hikjik committed Oct 17, 2023
1 parent f38759d commit 39fe388
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ add_task(minimum-operations-to-collect-elements)
add_task(minimum-operations-to-reduce-x-to-zero)
add_task(minimum-path-sum)
add_task(minimum-penalty-for-a-shop)
add_task(minimum-recolors-to-get-k-consecutive-black-blocks)
add_task(minimum-remove-to-make-valid-parentheses)
add_task(minimum-replacements-to-sort-the-array)
add_task(minimum-right-shifts-to-sort-the-array)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_catch(test_minimum_recolors_to_get_k_consecutive_black_blocks test.cpp)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <string>

class Solution {
public:
static int minimumRecolors(std::string blocks, int k) {
const int n = blocks.size();
int cnt = 0, max = 0;
for (int i = 0; i < n; ++i) {
if (blocks[i] == 'B') {
++cnt;
}
if (i >= k && blocks[i - k] == 'B') {
--cnt;
}
max = std::max(max, cnt);
}
return k - max;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <catch.hpp>

#include <solution.hpp>

TEST_CASE("Simple") {
struct TestCase {
std::string blocks;
int k;
int expected;
};

std::vector<TestCase> test_cases{
{
.blocks = "WBBWWBBWBW",
.k = 7,
.expected = 3,
},
{
.blocks = "WBWBBBW",
.k = 2,
.expected = 0,
},
};

for (const auto &[blocks, k, expected] : test_cases) {
const auto actual = Solution::minimumRecolors(blocks, k);
REQUIRE(expected == actual);
}
}

0 comments on commit 39fe388

Please sign in to comment.