Trains the technique from
LeetCode 566Reshape the MatrixThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A photo lab keeps a contact sheet as a grid sheet of m rows by n columns, where sheet[r][c] is the exposure reading of one frame.
The lab wants the very same frames pasted onto a fresh sheet with rows rows and cols columns. Frames come off the old sheet in reading order: the top row left to right, then the next row left to right, and so on. They go onto the new sheet in that same order and in the same reading pattern, filling the first row left to right before starting the second.
If the new layout does not hold exactly as many frames as the old sheet, the relay cannot be done and the original sheet is returned unchanged. Otherwise return the relaid sheet.
Example 1
The frames come off in the order 7, 2, 9, 4, 1, 6 and go onto three rows of two, so the first new row reads 7 and 2.
Example 2
Four rows of two would hold eight frames while the old sheet has six, so the sheet comes back as it was.
Example 3
All four frames land on a single row in reading order.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def relay_sheet(sheet: list[list[int]], rows: int, cols: int) -> list[list[int]]:public int[][] relaySheet(int[][] sheet, int rows, int cols)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.