Ctrl K

Four-Corner Rectangles

Medium
You are given a grid field of zeros and ones. A rectangle here is a set of four distinct cells holding 1 that are the corners of a rectangle whose sides run parallel to the edges of the grid. In other words, you pick two different rows and two different columns, and all four crossings must hold a 1. What lies inside the rectangle or along its sides does not matter: only the corners are checked. Return the number of such rectangles.
Example 1
field=[[1, 0, 1], [0, 1, 0], [1, 0, 1]] → 1

The only rectangle uses the four corners of the field: cells (0, 0), (0, 2), (2, 0) and (2, 2). The 1 in the middle belongs to no rectangle.

Example 2
field=[[1, 1], [1, 1], [1, 1]] → 3

Any two of the three rows give a rectangle on both columns: 3 ways in total.

Example 3
field=[[1, 1, 1, 1, 1]] → 0

A single row cannot supply four distinct corners.

Constraints
  • 1 ≤ field.length, field[i].length ≤ 200
  • All rows have the same length
  • field[i][j] is 0 or 1
  • The grid holds between 1 and 6000 ones
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMouse, Cat and Cheese HardTurn a Square Clockwise MediumWarehouse Cart Routes MediumCheapest Corner-to-Corner Route MediumLargest Solid Square Medium
loading Python…
Input
field=[[1, 0, 1], [0, 1, 0], [1, 0, 1]]
Expected
1

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