A solar farm is laid out as a grid of panels arranged in R rows and C columns. Every panel reports its power output in watts, except that a malfunctioning panel reports exactly -1 instead of a real reading. All panels in the same column share a single string inverter, so as a stand-in for any malfunctioning reading, engineers use the highest valid (non -1) reading recorded anywhere else in that same column. It is guaranteed that every column has at least one panel that did not malfunction.
Given the grid of readings, replace every -1 with the maximum valid reading in its column, leaving every other reading unchanged, and print the repaired grid.
The first line contains two integers R and C — the number of rows and columns.
Each of the next R lines contains C integers, the readings of that row, where each value is either -1 (a malfunctioning panel) or an integer in the range 0 to 100000 (a valid reading).
Print R lines, each containing C integers separated by single spaces: the repaired grid, in the same row-major order as the input.
Example 1
Input
3 3 3 -1 4 -1 5 -1 2 -1 6
Expected
3 5 4 3 5 6 2 5 6
Explanation
Column 0 has valid readings 3 and 2, so its max is 3, filling the -1 at row 1. Column 1 has only one valid reading, 5 (rows 0 and 2 are -1), so its max is 5, filling both. Column 2 has valid readings 4 and 6, so its max is 6, filling the -1 at row 1. The repaired grid is: row 0 becomes 3 5 4, row 1 becomes 3 5 6, row 2 becomes 2 5 6.
Example 2
Input
1 4 10 20 30 40
Expected
10 20 30 40
Explanation
There are no malfunctioning panels (no -1 values) anywhere in this single row, so every column's maximum is simply its one existing reading, and the grid is printed back unchanged: 10 20 30 40.
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 →