Repaint the Fence
A long garden fence has boards painted in different colours over the years. Each colour is a capital letter, so the fence reads like "GGRGBBG".
An artist wants an unbroken one-colour stretch as a mural background. She has paint for only k boards, and can repaint a board any colour. How long a one-colour stretch can she make?
Your task
Write longestOneColour(boards, k). You may change at most k characters of boards, each to any capital letter. Return the length of the longest run of consecutive equal characters you can make.
An empty fence gives 0.
longestOneColour(boards: string, k: int) → int
Examples
boards = "GGRGBBG" k = 1
4
Repaint the R green: the first four boards become GGGG, using the one repaint. GGRGB would need two repaints (the R and the B). The answer is 4.
boards = "RBRBRB" k = 2
5
Repainting the two B's in RBRBR makes RRRRR, length 5. Every 6-board stretch has three of each colour, so it would need three repaints.
Constraints
- 0 ≤ boards.length ≤ 10,000
- 0 ≤ k ≤ boards.length
- boards contains capital letters A-Z only.
Start with brute force
Check every stretch: count its colours, and if length - mostCommon ≤ k, record the length.
A 10,000-board fence has about 50 million stretches. Even updating counts as the end moves, that is O(n²) work, mostly on stretches that are obviously too short or too expensive.
Hints
0 of 4 shown- Hint 1
Pick a stretch, like
GGRGB. How many repaints make it all one colour, and which colour should you choose? - Hint 2
Keep the stretch's most common colour and repaint the rest. So a stretch works when
length - (count of its most common colour) ≤ k. - Hint 3
That rule lets you use a window. Grow it on the right. When it needs more than
krepaints, slide the left edge forward one board. - Hint 4
Keep colour counts for the window and the highest count seen so far,
top. For eachright: count the new board and updatetop. WhilewindowLength - top > k, remove the left board from the counts and moveleft. Record the window length. (topnever needs to go down: a smaller top can never beat the best length already found.)
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
A stretch needs length - mostCommon repaints: keep the commonest colour, repaint the others. So slide a window and keep it within budget.
Trace "GGRGBBG" with k = 1:
- Add G and G. The window is
GG, top is 2, and 0 repaints are needed, so the best length is 2. - Add R. The window is
GGR, top is 2, and 1 repaint is needed, so the best length is 3. - Add G. The window is
GGRG, top is 3, and 1 repaint is needed, so the best length is 4. - Add B:
GGRGB, top 3, repaints 2. Too many. Drop the left G:GRGB, length 4, repaints 4 - 3 = 1 by the rule. Best stays 4. - Add B: length 5, repaints 2. Drop G:
RGBB, length 4. Best stays 4. - Add G: length 5, repaints 2. Drop R:
GBBG, length 4. Best stays 4.
In steps 4 to 6, top stayed 3 even after a G left the window, so the code thinks GRGB needs 1 repaint when it really needs 2. That is safe:
- With a stale
top, the window only slides at its current size, a length already proved possible. It never records a longer one. - The window grows only when a new board pushes
tophigher. Thentopis the real count of that board inside the window, so the longer window really is possible.
function longestOneColour(boards, k) {
const count = new Map(); // colour -> boards in the window
let left = 0, top = 0, best = 0;
for (let right = 0; right < boards.length; right++) {
const c = boards[right];
count.set(c, (count.get(c) || 0) + 1);
top = Math.max(top, count.get(c));
// Too many repaints needed: drop the leftmost board.
while (right - left + 1 - top > k) {
count.set(boards[left], count.get(boards[left]) - 1);
left++;
}
best = Math.max(best, right - left + 1);
}
return best;
}def longest_one_colour(boards, k):
count = {} # colour -> boards in the window
left = top = best = 0
for right, c in enumerate(boards):
count[c] = count.get(c, 0) + 1
top = max(top, count[c])
# Too many repaints needed: drop the leftmost board.
while right - left + 1 - top > k:
count[boards[left]] -= 1
left += 1
best = max(best, right - left + 1)
return bestRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.