Problem · Matrix
Minimum Replacements Required to Make a Matrix Balanced
Learn this problemProblem statement
You are given a matrix with exactly two rows and n columns. Every cell contains R, W, or ?.
In one replacement, choose a cell containing ? and replace it with either R or W. Question marks that are not needed may remain unchanged.
The matrix is balanced when every row and every column contains an equal number of R and W cells. Question marks are not counted. Return the minimum number of replacements required to make the matrix balanced.
Function
minimumReplacementsRequired(matrix: char[][]) → intExamples
Example 1
matrix = [['W','R','?','?','?'],['R','?','?','?','W']]return = 4Set the second cell of row 2 to W, the fifth cell of row 1 to R, and replace the two question marks in column 4 with W above R. The remaining ?/? column can stay unchanged. Each row then has two W and two R cells, and every column is balanced, for a total of 4 replacements.
Constraints
Standard Huge Constraints