All problems
0519EasyArrayMatrix

Isolated Pallets on the Rack

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1582Special Positions in a Binary 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 storage rack is laid out as a grid with m aisles and n columns. rack[i][j] is 1 when a pallet sits in the slot at aisle i, column j, and 0 when that slot is empty.

A pallet is isolated when no other pallet shares its aisle and no other pallet shares its column. Return how many isolated pallets the rack holds.

Examples

Example 1

Input
rack = [[1, 0, 0], [0, 0, 0], [0, 0, 1]]
Output
2

The pallet at aisle 0, column 0 has its aisle and its column to itself, and so does the pallet at aisle 2, column 2, so both are isolated.

Example 2

Input
rack = [[1, 0, 0, 0], [0, 0, 1, 0]]
Output
2

One pallet sits at aisle 0, column 0 and the other at aisle 1, column 2. Neither shares an aisle or a column with the other, so both are isolated.

Example 3

Input
rack = [[1, 0], [1, 0]]
Output
0

Both pallets sit in column 0, so each of them shares its column with the other and neither is isolated.

Constraints

  • m == rack.length
  • n == rack[i].length
  • 1 <= m, n <= 100
  • rack[i][j] is either 0 or 1.

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 count_isolated_pallets(rack: list[list[int]]) -> int:
Java
public int countIsolatedPallets(int[][] rack)
September 7
Apply