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.


// memory limit exceeded 
// my code 

class Solution {
    public int countCornerRectangles(int[][] grid) {
        // fix two rows and check columns 
        int res = 0;
        for(int i = 0; i < grid.length - 1; i++){ // starting row 
            // get an array of 1's index on this row
            List<Integer> oneInRow1 = getIndex(grid[i]);
            
            for(int j = i + 1; j < grid.length; j++){ // ending row 
                // get an array if 1's index on this row
                List<Integer> oneInRow2 = getIndex(grid[j]);
                // get all combination of 1's index on row 1 , and check if they exist in the row2 
                // if yes, res++, 
                List<Integer> commonIndex = commonIndex(oneInRow1, oneInRow2);
                // combination of any two from n points 
                int n = commonIndex.size();
                res += fac(n) / (fac(2) * fac(n - 2));
                
            }
        }
        return res;
    }
    private int fac(int n){
        int res = 1;
        for(int i = 1; i <= n; i++){
            res *= i;
        }
        return res;
    }
    private List<Integer> commonIndex (List<Integer> listOne, List<Integer> listTwo){
        List<Integer> result = new ArrayList<>();
        int i = 0;
        int j = 0;
        while(i < listOne.size() && j < listTwo.size()){
            if(listOne.get(i) == listTwo.get(j)){
                result.add(listOne.get(i));
            }else if(listOne.get(i) > listTwo.get(j)){
                j++;
            }else{
                i++;
            }
        }
        return result;
    }
    
    private List<Integer> getIndex(int[] row){
        List<Integer> result = new ArrayList<>();
        for(int i = 0; i < row.length; i++){
            if(row[i] == 1){
                result.add(i);
            }
        }
        return result;
    }

}


https://leetcode.com/problems/number-of-corner-rectangles/discuss/110196/short-JAVA-AC-solution-(O(m2-*-n))-with-explanation.




// correct, 
//我也这么想的, 有些地方写复杂了
// 公式也想复杂了

class Solution {
    public int countCornerRectangles(int[][] grid) {
        int res = 0;
        for(int i = 0; i < grid.length - 1; i++){
            for(int j = i + 1; j < grid.length; j++){
                int count = 0;
                for(int k = 0; k < grid[0].length; k++){
                    if(grid[i][k] == 1 && grid[j][k] == 1) count++;
                }
                if(count > 0) res += count * (count - 1) / 2;
            }
        }
        return res;
    }
}

 

posted on 2018-11-09 10:37  猪猪&#128055;  阅读(101)  评论(0)    收藏  举报

导航