Parking Lot Puddles
After a storm, a drone photographs a parking lot and splits the picture into a grid: 1 for a square under water, 0 for dry. The crew sends one person with a squeegee to each puddle, so they need the number of separate puddles.
Water squares that share an edge are the same puddle. Squares that touch only at a corner are not: water does not flow across those diagonal cracks.
Your task
Write countPuddles(lot). lot is a grid of 0s and 1s. Return the number of puddles: groups of 1s joined up, down, left or right.
A grid with no rows, or with no 1s, has 0 puddles.
countPuddles(lot: int[][]) → int
Examples
lot = [[1, 1, 0, 0], [0, 1, 0, 1], [0, 0, 0, 1], [1, 0, 0, 0]]
3
The three 1s in the top-left are one puddle, the two on the right edge a second, and the lone 1 in the bottom-left corner a third.
lot = [[1, 0, 1], [0, 1, 0], [1, 0, 1]]
5
Every wet square touches the others only at corners, so each is its own puddle.
Constraints
- 0 ≤ rows ≤ 20
- 1 ≤ cols ≤ 20 when rows ≥ 1
- lot[r][c] is 0 or 1
Start with brute force
A tempting shortcut: count a wet square as a new puddle when the squares above and to its left are both dry. A U-shaped puddle has two tops, so it gets counted twice. The rule sees two neighbors, but a puddle can wind in any direction.
The safe slow version gives every wet square its own label, then sweeps the grid repeatedly, giving neighbors the smaller of their labels until nothing changes, and counts distinct labels. That can take many sweeps. A search from each new puddle needs one pass.
Hints
0 of 4 shown- Hint 1
Stand on the top-left wet square of the first example. If you mopped up that whole puddle now, what would the grid look like?
- Hint 2
Counting puddles means counting how often you find a wet square that is not part of a puddle you already handled.
- Hint 3
When you find one, you must visit its whole puddle so you do not count it again. Which kind of search visits everything connected to a starting point?
- Hint 4
Scan the grid row by row. Each time you meet a 1, add one to the count and run a depth-first or breadth-first search from it that turns every connected 1 into 0 (or marks it seen). The scan then skips those squares, so each puddle is counted once.
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
Wet squares are nodes, joined when they share a side, and each puddle is a connected group. Count a group the first time you touch it, then visit all of it so you never count it again.
Trace the first example, scanning row by row:
- (0,0) is wet and unseen. Count = 1. The search reaches (0,1), then (1,1). All three are marked seen.
- (0,1) and (1,1) come up later in the scan but are already seen, so they are skipped.
- (1,3) is wet and unseen. Count = 2. The search reaches (2,3) below it.
- (3,0) is wet and unseen. Count = 3. It has no wet neighbors.
The U-shaped trap cannot fool this: the search from the first top walks down, along the bottom and up the other arm, so the second top is already seen when the scan reaches it.
function countPuddles(lot) {
const rows = lot.length;
if (rows === 0) return 0;
const cols = lot[0].length;
const seen = lot.map(row => row.map(() => false));
let count = 0;
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (lot[r][c] !== 1 || seen[r][c]) continue;
count++; // a puddle we have not met before
seen[r][c] = true;
const stack = [[r, c]];
while (stack.length > 0) {
const [cr, cc] = stack.pop();
for (const [nr, nc] of [[cr + 1, cc], [cr - 1, cc], [cr, cc + 1], [cr, cc - 1]]) {
if (nr < 0 || nr >= rows || nc < 0 || nc >= cols) continue;
if (lot[nr][nc] !== 1 || seen[nr][nc]) continue;
seen[nr][nc] = true;
stack.push([nr, nc]);
}
}
}
}
return count;
}def count_puddles(lot):
rows = len(lot)
if rows == 0:
return 0
cols = len(lot[0])
seen = [[False] * cols for _ in range(rows)]
count = 0
for r in range(rows):
for c in range(cols):
if lot[r][c] != 1 or seen[r][c]:
continue
count += 1 # a puddle we have not met before
seen[r][c] = True
stack = [(r, c)]
while stack:
cr, cc = stack.pop()
for nr, nc in ((cr + 1, cc), (cr - 1, cc), (cr, cc + 1), (cr, cc - 1)):
if 0 <= nr < rows and 0 <= nc < cols and lot[nr][nc] == 1 and not seen[nr][nc]:
seen[nr][nc] = True
stack.append((nr, nc))
return countRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.