-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimum_recolors.cpp
More file actions
61 lines (56 loc) · 1.38 KB
/
minimum_recolors.cpp
File metadata and controls
61 lines (56 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* =====================================================================================
*
* Filename: minimum_recolors.cpp
*
* Description: 2379. Minimum Recolors to Get K Consecutive Black Blocks
*
* Version: 1.0
* Created: 03/08/2025 22:53:18
* Revision: none
* Compiler: gcc
*
* Author: xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <algorithm>
#include <string>
#include <tuple>
#include <vector>
#include "gtest/gtest.h"
using std::string;
class Solution {
public:
int minimumRecolors(string blocks, int k) {
int black_cnt = 0;
int max_cnt = 0;
for (int l = 0, r = 0; r < blocks.length(); r++) {
if (r < k) {
if (blocks[r] == 'B') {
max_cnt = ++black_cnt;
}
} else {
if (blocks[r] == 'B') {
black_cnt++;
}
if (blocks[l] == 'B') {
black_cnt--;
}
max_cnt = std::max(max_cnt, black_cnt);
l++;
}
}
return k - max_cnt;
}
};
TEST(Solution, minimumRecolors) {
std::vector<std::tuple<string, int, int>> cases = {
std::make_tuple("WBBWWBBWBW", 7, 3),
std::make_tuple("WBWBBBW", 2, 0),
};
for (auto& [blocks, k, num] : cases) {
EXPECT_EQ(Solution().minimumRecolors(blocks, k), num);
}
}