|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -/** |
4 |
| - * 750. Number Of Corner Rectangles |
5 |
| - * |
6 |
| - * Given a grid where each entry is only 0 or 1, find the number of corner rectangles. |
7 |
| - * A corner rectangle is 4 distinct 1s on the grid that form an axis-aligned rectangle. |
8 |
| - * Note that only the corners need to have the value 1. Also, all four 1s used must be distinct. |
9 |
| -
|
10 |
| - Example 1: |
11 |
| - Input: grid = |
12 |
| - [[1, 0, 0, 1, 0], |
13 |
| - [0, 0, 1, 0, 1], |
14 |
| - [0, 0, 0, 1, 0], |
15 |
| - [1, 0, 1, 0, 1]] |
16 |
| - Output: 1 |
17 |
| - Explanation: There is only one corner rectangle, with corners grid[1][2], grid[1][4], grid[3][2], grid[3][4]. |
18 |
| -
|
19 |
| - Example 2: |
20 |
| - Input: grid = |
21 |
| - [[1, 1, 1], |
22 |
| - [1, 1, 1], |
23 |
| - [1, 1, 1]] |
24 |
| - Output: 9 |
25 |
| - Explanation: There are four 2x2 rectangles, four 2x3 and 3x2 rectangles, and one 3x3 rectangle. |
26 |
| -
|
27 |
| - Example 3: |
28 |
| - Input: grid = |
29 |
| - [[1, 1, 1, 1]] |
30 |
| - Output: 0 |
31 |
| - Explanation: Rectangles must have four distinct corners. |
32 |
| -
|
33 |
| - Note: |
34 |
| - The number of rows and columns of grid will each be in the range [1, 200]. |
35 |
| - Each grid[i][j] will be either 0 or 1. |
36 |
| - The number of 1s in the grid will be at most 6000.*/ |
37 | 3 | public class _750 {
|
38 |
| - public static class Solution1 { |
39 |
| - public int countCornerRectangles(int[][] grid) { |
40 |
| - if (grid == null || grid.length < 2) { |
41 |
| - return 0; |
42 |
| - } |
43 |
| - int m = grid.length; |
44 |
| - int n = grid[0].length; |
45 |
| - int count = 0; |
46 |
| - for (int i = 0; i < m - 1; i++) { |
47 |
| - for (int j = 0; j < n - 1; j++) { |
48 |
| - if (grid[i][j] == 1) { |
49 |
| - for (int jNext = j + 1; jNext < n; jNext++) { |
50 |
| - if (grid[i][jNext] == 1) { |
51 |
| - for (int iNext = i + 1; iNext < m; iNext++) { |
52 |
| - if (grid[iNext][j] == 1 && grid[iNext][jNext] == 1) { |
53 |
| - count++; |
54 |
| - } |
| 4 | + public static class Solution1 { |
| 5 | + public int countCornerRectangles(int[][] grid) { |
| 6 | + if (grid == null || grid.length < 2) { |
| 7 | + return 0; |
| 8 | + } |
| 9 | + int m = grid.length; |
| 10 | + int n = grid[0].length; |
| 11 | + int count = 0; |
| 12 | + for (int i = 0; i < m - 1; i++) { |
| 13 | + for (int j = 0; j < n - 1; j++) { |
| 14 | + if (grid[i][j] == 1) { |
| 15 | + for (int jNext = j + 1; jNext < n; jNext++) { |
| 16 | + if (grid[i][jNext] == 1) { |
| 17 | + for (int iNext = i + 1; iNext < m; iNext++) { |
| 18 | + if (grid[iNext][j] == 1 && grid[iNext][jNext] == 1) { |
| 19 | + count++; |
| 20 | + } |
| 21 | + } |
| 22 | + } |
| 23 | + } |
| 24 | + } |
55 | 25 | }
|
56 |
| - } |
57 | 26 | }
|
58 |
| - } |
| 27 | + return count; |
59 | 28 | }
|
60 |
| - } |
61 |
| - return count; |
62 | 29 | }
|
63 |
| - } |
64 | 30 | }
|
0 commit comments