750.Number Of Corner Rectangles

1-Math combination

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

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

https://leetcode.com/problems/number-of-corner-rectangles/discuss/110200/Summary-of-three-solutions-based-on-three-different-ideas

results matching ""

    No results matching ""