package com.fishercoder.solutions;

/**
 * 750. Number Of Corner Rectangles
 *
 * Given a grid where each entry is only 0 or 1, find the number of corner rectangles.
 * A corner rectangle is 4 distinct 1s on the grid that form an axis-aligned rectangle.
 * Note that only the corners need to have the value 1. Also, all four 1s used must be distinct.

 Example 1:
 Input: grid =
 [[1, 0, 0, 1, 0],
 [0, 0, 1, 0, 1],
 [0, 0, 0, 1, 0],
 [1, 0, 1, 0, 1]]
 Output: 1
 Explanation: There is only one corner rectangle, with corners grid[1][2], grid[1][4], grid[3][2], grid[3][4].

 Example 2:
 Input: grid =
 [[1, 1, 1],
 [1, 1, 1],
 [1, 1, 1]]
 Output: 9
 Explanation: There are four 2x2 rectangles, four 2x3 and 3x2 rectangles, and one 3x3 rectangle.

 Example 3:
 Input: grid =
 [[1, 1, 1, 1]]
 Output: 0
 Explanation: Rectangles must have four distinct corners.

 Note:
 The number of rows and columns of grid will each be in the range [1, 200].
 Each grid[i][j] will be either 0 or 1.
 The number of 1s in the grid will be at most 6000.*/
public class _750 {
  public static class Solution1 {
    public int countCornerRectangles(int[][] grid) {
      if (grid == null || grid.length < 2) {
        return 0;
      }
      int m = grid.length;
      int n = grid[0].length;
      int count = 0;
      for (int i = 0; i < m - 1; i++) {
        for (int j = 0; j < n - 1; j++) {
          if (grid[i][j] == 1) {
            for (int jNext = j + 1; jNext < n; jNext++) {
              if (grid[i][jNext] == 1) {
                for (int iNext = i + 1; iNext < m; iNext++) {
                  if (grid[iNext][j] == 1 && grid[iNext][jNext] == 1) {
                    count++;
                  }
                }
              }
            }
          }
        }
      }
      return count;
    }
  }
}

@@ -33,1010 +33,8 @@
 s;%0A%0A
-/**%0A * 750. Number Of Corner Rectangles%0A *%0A * Given a grid where each entry is only 0 or 1, find the number of corner rectangles.%0A * A corner rectangle is 4 distinct 1s on the grid that form an axis-aligned rectangle.%0A * Note that only the corners need to have the value 1. Also, all four 1s used must be distinct.%0A%0A Example 1:%0A Input: grid =%0A %5B%5B1, 0, 0, 1, 0%5D,%0A %5B0, 0, 1, 0, 1%5D,%0A %5B0, 0, 0, 1, 0%5D,%0A %5B1, 0, 1, 0, 1%5D%5D%0A Output: 1%0A Explanation: There is only one corner rectangle, with corners grid%5B1%5D%5B2%5D, grid%5B1%5D%5B4%5D, grid%5B3%5D%5B2%5D, grid%5B3%5D%5B4%5D.%0A%0A Example 2:%0A Input: grid =%0A %5B%5B1, 1, 1%5D,%0A %5B1, 1, 1%5D,%0A %5B1, 1, 1%5D%5D%0A Output: 9%0A Explanation: There are four 2x2 rectangles, four 2x3 and 3x2 rectangles, and one 3x3 rectangle.%0A%0A Example 3:%0A Input: grid =%0A %5B%5B1, 1, 1, 1%5D%5D%0A Output: 0%0A Explanation: Rectangles must have four distinct corners.%0A%0A Note:%0A The number of rows and columns of grid will each be in the range %5B1, 200%5D.%0A Each grid%5Bi%5D%5Bj%5D will be either 0 or 1.%0A The number of 1s in the grid will be at most 6000.*/%0A
 publ
@@ -49,16 +49,18 @@
  _750 %7B%0A
+  
   public
@@ -85,16 +85,20 @@
 tion1 %7B%0A
+    
     publ
@@ -144,24 +144,30 @@
 id) %7B%0A      
+     
+ 
 if (grid == 
@@ -189,24 +189,32 @@
 ngth %3C 2) %7B%0A
+        
         retu
@@ -225,24 +225,36 @@
 ;%0A      
+  
-%7D%0A
+    %7D%0A     
       
+ 
 int m = 
@@ -268,24 +268,30 @@
 ngth;%0A      
+    
+  
 int n = grid
@@ -304,24 +304,30 @@
 ngth;%0A      
+   
+   
 int count = 
@@ -325,24 +325,30 @@
  count = 0;%0A
+      
       for (i
@@ -371,24 +371,32 @@
 - 1; i++) %7B%0A
+        
         for 
@@ -421,24 +421,34 @@
 - 1; j++) %7B%0A
+          
           if
@@ -472,32 +472,44 @@
 ) %7B%0A            
+  
+          
 for (int jNext =
@@ -544,32 +544,46 @@
 %7B%0A              
+         
+     
 if (grid%5Bi%5D%5BjNex
@@ -601,32 +601,48 @@
                 
+               
+ 
 for (int iNext =
@@ -663,32 +663,50 @@
 %3C m; iNext++) %7B%0A
+                  
                 
@@ -753,32 +753,52 @@
 %5BjNext%5D == 1) %7B%0A
+                    
                 
@@ -810,16 +810,34 @@
 ount++;%0A
+                  
         
@@ -868,60 +868,119 @@
     
+ 
-%7D%0A              %7D%0A            %7D%0A          %7D%0A
+               %7D%0A                            %7D%0A                        %7D%0A                    %7D%0A       
         
 %7D%0A  
@@ -975,24 +975,25 @@
         
+ 
 %7D%0A      
 %7D%0A      
@@ -984,24 +984,36 @@
 %7D%0A      
+    
-%7D%0A
+  %7D%0A   
       
+   
 return c
@@ -1022,16 +1022,22 @@
 nt;%0A    
-%7D%0A
+    %7D%0A  
   %7D%0A%7D%0A

refactor 750