No-Repeat Garden Beds
A community garden is a 9 by 9 grid of plots, split into nine 3 by 3 beds. There are nine kinds of plant, numbered 1 to 9. To stop pests spreading, no kind may appear twice in the same row, column or bed.
The planting plan is partly filled in, with 0 for an unclaimed plot. Check that what is planned so far breaks no rule.
Your task
Write validPlan(plots). plots is a 9 × 9 grid, plots[r][c] being the plant in row r, column c, or 0 if the plot is empty. Bed boundaries fall after rows 2 and 5 and after columns 2 and 5.
Return true if no plant number (1 to 9) appears twice in any row, column or bed, and false otherwise. Empty plots never break a rule. Only check the planned plots, not whether the empty ones could all be filled.
validPlan(plots: int[][]) → bool
Examples
plots = [[0, 2, 0, 0, 0, 0, 0, 0, 9], [0, 5, 6, 0, 8, 9, 1, 2, 0], [0, 0, 0, 1, 0, 0, 4, 0, 6], [0, 3, 4, 0, 0, 0, 0, 0, 0], [5, 6, 7, 0, 9, 1, 0, 3, 0], [0, 9, 0, 0, 0, 4, 0, 0, 7], [0, 0, 5, 0, 7, 8, 0, 0, 0], [6, 0, 8, 0, 1, 2, 0, 0, 0], [0, 1, 0, 3, 0, 0, 6, 7, 0]]
true
Each row, column and bed holds each plant at most once. Empty plots are fine.
plots = [[0, 2, 0, 0, 0, 0, 0, 0, 9], [0, 5, 6, 0, 8, 9, 1, 2, 0], [2, 0, 0, 1, 0, 0, 4, 0, 6], [0, 3, 4, 0, 0, 0, 0, 0, 0], [5, 6, 7, 0, 9, 1, 0, 3, 0], [0, 9, 0, 0, 0, 4, 0, 0, 7], [0, 0, 5, 0, 7, 8, 0, 0, 0], [6, 0, 8, 0, 1, 2, 0, 0, 0], [0, 1, 0, 3, 0, 0, 6, 7, 0]]
false
This is the first plan plus a 2 at row 2, column 0. Row 2 (1, 4, 6) and column 0 (5, 6) are fine, but the top-left bed already has a 2 at row 0, column 1.
Constraints
- plots has exactly 9 rows, each with exactly 9 numbers.
- 0 ≤ plots[r][c] ≤ 9
Start with brute force
For every planted plot, scan the rest of its row, column and bed for the same plant.
On a 9 × 9 grid that is about 81 × 24 checks, so speed is not a problem. The drawback is the code, which needs three scanning loops and careful bed boundaries, and on an n × n garden the work grows like n³. Recording each plant once in its three groups is shorter and faster.
Hints
0 of 4 shown- Hint 1
Take the plot at row 2, column 0 in the second example. Which three groups of plots decide whether its plant breaks a rule?
- Hint 2
Every plot belongs to one row, one column and one bed: 27 groups in all. For each, you ask "have I seen this plant in this group before?"
- Hint 3
Can you number the beds 0 to 8 from
randc, the way rows and columns are numbered? Divide each by 3, rounding down, and try a few plots. - Hint 4
Keep 9 sets each for rows, columns and beds, with bed number
(r ÷ 3, rounded down) × 3 + (c ÷ 3, rounded down). Visit every plot once and skip zeros. If the plant is already in its row, column or bed set, returnfalse; otherwise add it to all three. Returntrueat the end.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
The rule is 27 small duplicate checks, one per row, column and bed. Give each group its own set, visit every plot once, and drop its plant into its three sets.
The tricky part is the bed number. Math.floor(r / 3) (r // 3 in Python) gives the band of rows, 0 to 2; the same with c gives the band of columns. Treat them as the row and column of a 3 × 3 grid of beds: bed = band row × 3 + band column. Plot (4, 7) is in band row 1, band column 2, so bed 1 × 3 + 2 = 5.
Trace the start of the second example, row by row:
- The plot (0, 1) holds a 2 and is in bed 0, so row 0, column 1 and bed 0 all record a 2.
- The plot (0, 8) holds a 9 and is in bed 2, so the 9 is recorded in row 0, column 8 and bed 2.
- Row 1 adds 5, 6, 8, 9, 1 and 2 with no repeats, and bed 0 now holds 2, 5 and 6.
- The plot (2, 0) holds a 2 and is in bed 0. Row 2 and column 0 have no 2 yet, but bed 0 already does, so the function returns
false.
A common slip is r / 3 + c / 3 as the bed number. It puts plots (0, 4) and (4, 0) in the same "bed" 1, though they are in different beds, and flags repeats that are not there.
function validPlan(plots) {
const rows = [], cols = [], beds = [];
for (let i = 0; i < 9; i++) { rows.push(new Set()); cols.push(new Set()); beds.push(new Set()); }
for (let r = 0; r < 9; r++) {
for (let c = 0; c < 9; c++) {
const v = plots[r][c];
if (v === 0) continue; // empty plot
const b = Math.floor(r / 3) * 3 + Math.floor(c / 3); // bed 0..8
if (rows[r].has(v) || cols[c].has(v) || beds[b].has(v)) return false;
rows[r].add(v); cols[c].add(v); beds[b].add(v);
}
}
return true;
}def valid_plan(plots):
rows = [set() for _ in range(9)]
cols = [set() for _ in range(9)]
beds = [set() for _ in range(9)]
for r in range(9):
for c in range(9):
v = plots[r][c]
if v == 0: # empty plot
continue
b = (r // 3) * 3 + c // 3 # bed 0..8
if v in rows[r] or v in cols[c] or v in beds[b]:
return False
rows[r].add(v)
cols[c].add(v)
beds[b].add(v)
return TrueRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.