All problems
0495EasyArrayMatrixSimulation

Relaying the Contact Sheet

Tracked in this browser only
Write code

Trains the technique from

LeetCode 566Reshape the Matrix

This 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.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def relay_sheet(sheet: list[list[int]], rows: int, cols: int) -> list[list[int]]:
Java
public int[][] relaySheet(int[][] sheet, int rows, int cols)
September 7
Apply