Ctrl K

Corners Made of Ones

Medium
You are given a rectangular table grid of zeros and ones. Call a *corner* any set of three different cells holding ones where one cell (the vertex) shares a row with the second cell and a column with the third. The cells do not have to be adjacent. Return the number of corners in the table. Two triples made of the same three cells count as one corner.
Example 1
grid=[[1, 1, 0], [0, 1, 0], [1, 0, 1]] → 3

Three triples work: corner (0, 0) with (0, 1) and (2, 0); corner (0, 1) with (0, 0) and (1, 1); corner (2, 0) with (2, 2) and (0, 0).

Example 2
grid=[[1, 0], [0, 1]] → 0

There are only two ones, and they share neither a row nor a column.

Example 3
grid=[[1, 1], [1, 1]] → 4

Each of the four ones can be the corner, and the other two are then fixed, giving 4 triples.

Constraints
  • 1 ≤ number of rows, number of columns ≤ 1000
  • grid[i][j] is 0 or 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPair GCDs by Sorted Position HardPairs Balanced by Their Mirrors MediumPhoto Frames of the Same Shape MediumPairs Off the Pace MediumNumbers and Their Mirrors Medium
loading Python…
Input
grid=[[1, 1, 0], [0, 1, 0], [1, 0, 1]]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 70 tests on the server, hidden ones included.