Convert 1D Array Into 2D Array
Real-world version: Laying the Tiles Into a RackThe statement here is our own: the same inputs, outputs and technique.
A run of tiles is to be laid into a rack of rows shelves, each shelf holding cols slots. Fill the rack shelf by shelf from the top, and within each shelf from left to right, taking the tiles in the order they are given.
Return the filled rack. When the run of tiles does not fill the rack exactly, return an empty list.
Examples
Example 1
- Input
- tiles = [1, 2, 3, 4, 5, 6], rows = 2, cols = 3
- Output
- [[1, 2, 3], [4, 5, 6]]
Six tiles fill two shelves of three slots, taken in order along the top shelf and then the bottom one.
Example 2
- Input
- tiles = [1, 2, 3, 4, 5], rows = 2, cols = 2
- Output
- []
Five tiles cannot fill a rack of four slots, whichever way they are laid, so nothing is returned.
Example 3
- Input
- tiles = [5, 4, 3, 2, 1], rows = 5, cols = 1
- Output
- [[5], [4], [3], [2], [1]]
Five shelves of a single slot take the tiles one at a time in the order given.
Constraints
- 1 <= tiles.length <= 5 * 10^4
- 1 <= tiles[i] <= 10^5
- 1 <= rows <= 4 * 10^4
- 1 <= cols <= 4 * 10^4
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 construct2_d_array(tiles: list[int], rows: int, cols: int) -> list[list[int]]:public int[][] construct2DArray(int[] tiles, int rows, int cols)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 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
- Reshape the MatrixReal-world version: Relaying the Contact SheetEasy
- 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