Reshape the Matrix
Real-world version: Relaying the Contact SheetThe statement here is our own: the same inputs, outputs and technique.
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.
Examples
Example 1
- Input
- sheet = [[7, 2, 9], [4, 1, 6]], rows = 3, cols = 2
- Output
- [[7, 2], [9, 4], [1, 6]]
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
- Input
- sheet = [[7, 2, 9], [4, 1, 6]], rows = 4, cols = 2
- Output
- [[7, 2, 9], [4, 1, 6]]
Four rows of two would hold eight frames while the old sheet has six, so the sheet comes back as it was.
Example 3
- Input
- sheet = [[-1000, 1000], [0, -7]], rows = 1, cols = 4
- Output
- [[-1000, 1000, 0, -7]]
All four frames land on a single row in reading order.
Constraints
- m == sheet.length
- n == sheet[i].length
- 1 <= m, n <= 100
- -1000 <= sheet[r][c] <= 1000
- 1 <= rows, cols <= 300
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard 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)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 11 test cases: 11 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Transpose MatrixReal-world version: Flip a Reading Grid Onto Its SideEasy
- Shift 2D GridReal-world version: Rolling the Tile Board ForwardEasy
- Find Winner on a Tic Tac Toe GameReal-world version: Who Comes Out Ahead on the Three-by-ThreeEasy
- Convert 1D Array Into 2D ArrayReal-world version: Laying the Tiles Into a RackEasy