A stadium's marquee display is a grid of m rows and n columns of digit tiles, where each tile currently shows a single digit from 0 to 9. The marquee's controller wants to repaint some tiles (each repaint changes one tile to any digit of choice, and counts as one operation) so that the final grid satisfies two rules:
Determine the minimum total number of tile repaints needed to reach a grid satisfying both rules.
The first line contains two integers m and n — the number of rows and columns.
Each of the next m lines contains n space-separated integers, the digits of that row of the grid (each in the range 0 to 9).
A single integer: the minimum number of tile repaints required.
Example 1
Input
2 2 3 3 3 4
Expected
1
Explanation
Column 0 is already all 3s (cost 0 to keep it 3). Column 1 has one 3 and one 4; setting it to 4 costs 1 (repaint the single 3 in that column) and 4 differs from column 0's 3, satisfying the adjacency rule. Total repaints: 1, which is optimal.
Example 2
Input
1 3 5 5 5
Expected
1
Explanation
With one row, every column already costs 0 if kept at 5, but all three columns being 5 would violate the adjacency rule between columns 0-1 and 1-2. Repainting only the middle column to any digit other than 5 (cost 1) leaves column 0 = 5, column 1 = (something else), column 2 = 5, and every adjacent pair differs. Total repaints: 1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →