Show problems as
Classic problem #1582EasyArrayMatrix

Special Positions in a Binary Matrix

Real-world version: Isolated Pallets on the Rack
Tracked in this browser only
Read Special Positions in a Binary Matrix on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def count_isolated_pallets(rack: list[list[int]]) -> int:
Java
public int countIsolatedPallets(int[][] rack)