PROBLEM LINK:
Author:Sunny Aggarwal
Tester:Sergey Kulik
Editorialist:Mugurel Ionut Andreica
DIFFICULTY:
CAKEWALK
PREREQUISITES:
None
PROBLEM:
Chef has N rooms, each of them painted in one of 3 colors (R, G or B). He wants to repaint the minimum number of rooms such that, in the end, all the rooms have the same color.
QUICK EXPLANATION:
Paint all the rooms in the color which appears the largest number of times (of course, rooms which already have that color will not be repainted).
EXPLANATION:
Repainting the minimum number of rooms is equivalent to not repainting (i.e. keeping the initial color in) the maximum number of rooms. Since all the rooms must have the same color in the end, this means that we should find the color C in which the most rooms are painted and then repaint all the rooms which have a color other than C in color C.
In order to count how many rooms of each color we have, we can simply maintain three variables cntR, cntG and cntB. Then we traverse the string denoting the colors of the rooms and increment the corresponding variable depending on the color denoted by the current character in the string.
Time complexity: O(N).
AUTHOR'S AND TESTER'S SOLUTIONS:
Author's solution can be found here.
Tester's solution can be found here.