-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minimum Recolors to Get K Consecutive Black Blocks
- Loading branch information
Showing
4 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
solutions/minimum-recolors-to-get-k-consecutive-black-blocks/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
21 changes: 21 additions & 0 deletions
21
solutions/minimum-recolors-to-get-k-consecutive-black-blocks/solution.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
29 changes: 29 additions & 0 deletions
29
solutions/minimum-recolors-to-get-k-consecutive-black-blocks/test.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |