Fill a Grid With Connected Crop Strips in Snake Order
Arrays & HashingAuto-graded · 21 test casesLast updated
Build an N by M grid in which each crop forms one edge-connected region, using a snake-order fill with a fixed, deterministic output.
A field is laid out as a grid with n rows and m columns. There are k crop types, numbered 1 to k, where k = len(counts). Crop i (1-indexed) must be planted in exactly counts[i - 1] cells. The counts add up to n * m, so every cell gets exactly one crop.
Each crop with a positive count must form a single connected region. Two cells are connected only if they share an edge (up, down, left or right). Diagonal contact does not count.
Because the result is checked automatically, your function must return one specific arrangement, built with the snake-order fill below:
- Visit the cells in snake order. Row 0 goes left to right, row 1 goes right to left, row 2 goes left to right, and so on.
- Assign crop labels along this visiting order in increasing label order. The first
counts[0]visited cells get label1, the nextcounts[1]cells get label2, and so on. - A crop with count
0is skipped and does not appear in the grid.
Return the grid as a list of n rows, each a list of m integer labels, where each entry is the label at that cell's actual position (column index counted from the left).
Consecutive cells in snake order always share an edge, including where one row ends and the next begins. Every crop therefore occupies a contiguous stretch of the order and is connected.
Constraints
1 <= n,1 <= mcountscontains non-negative integers andsum(counts) == n * m- Labels are 1-indexed, so
counts[0]belongs to crop1 - The grid may be a single row or a single column
- Crops with count
0do not appear in the output
Examples
Example 1
Input: fill_crop_regions(2, 3, [2, 2, 2])
Output: [[1, 1, 2], [3, 3, 2]]
Example 2
Input: fill_crop_regions(3, 4, [0, 5, 1, 6])
Output: [[2, 2, 2, 2], [4, 4, 3, 2], [4, 4, 4, 4]]
Example 3
Input: fill_crop_regions(1, 7, [3, 0, 4])
Output: [[1, 1, 1, 3, 3, 3, 3]]
Example 4
Input: fill_crop_regions(4, 1, [1, 0, 2, 1])
Output: [[1], [3], [3], [4]]
Hints
Hint 1
Think of the grid as one long sequence of n*m cells. First work out which crop label belongs at each position of that sequence, ignoring the 2D layout.
Hint 2
Walking the rows left-to-right, then right-to-left alternately, means the last cell of one row sits directly above or below the first cell of the next in the visiting order. That is why a contiguous run of the sequence stays connected.
Hint 3
Build the flat label list by repeating each label counts[i] times (zero counts add nothing), cut it into rows of length m, then reverse every odd-indexed row to undo the snake direction.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Imagine walking through the grid in snake order: row 0 left to right, row 1 right to left, row 2 left to right, and so on. Any two consecutive cells in this walk share an edge. Within a row they are horizontal neighbours. At the end of a row, the walk moves down to the cell directly below, because the next row starts from the same column where the previous one ended.
The task becomes: lay out the labels in one flat sequence and cut it into rows.
- Build the flat list of labels by repeating label
i + 1exactlycounts[i]times, foriin increasing order. Zero counts add nothing. - Split the list into
nchunks of lengthm. Chunkrholds the labels visited during rowr. - Reverse every odd-indexed chunk. Those rows were visited right to left, so the label visited first belongs in the rightmost column.
Why it is correct
- Counts: the flat list contains label
i + 1exactlycounts[i]times, and its length issum(counts) = n * m. Every cell is filled exactly once. - Connectivity: each crop occupies a contiguous block of the snake order. Consecutive cells in that order are edge-adjacent, so the block is a connected path.
- Edge cases: a single row is just one left-to-right pass. A single column makes each row one cell, and reversing a one-element row changes nothing, so the walk simply goes downward. Zero counts add no elements. A count larger than
msimply spans several rows.
Complexity
- Time:
O(n * m + k), wherek = len(counts). - Space:
O(n * m)for the output grid and the flat label list.
Reference solution
from typing import List
def fill_crop_regions(n: int, m: int, counts: List[int]) -> List[List[int]]:
# Flat sequence of labels in snake-visiting order.
labels = []
for idx, c in enumerate(counts):
labels.extend([idx + 1] * c)
grid = []
for r in range(n):
row = labels[r * m:(r + 1) * m]
if r % 2 == 1:
row.reverse()
grid.append(row)
return grid