Input: mat = [[0, 1, 0, 1, 1, 0], [0, 1, 0, 1, 1, 0], [0, 1, 0, 1, 1, 0], [0, 0, 1, 0, 1, 0]], N = 3
Output: 6
Explanation: All the underlined '1' are the 1's we need (all '1's at column 1 and 3). [[0, 1, 0, 1, 1, 0], [0, 1, 0, 1, 1, 0], [0, 1, 0, 1, 1, 0], [0, 0, 1, 0, 1, 0]]
- Rule 1: For each row R and column C, both should have exactly N number of "1's" (where N = 3 in this case).
- Rule 2: If the rows containing "1's" at column C are the same as row R, they contribute to the count.
Let's go through an example for row R = 0 and column C = 1:
- Rule 1: Row R = 0 and column C = 1 both have exactly three "1's" in them.
- Rule 2: The rows that have "1's" at column C = 1 are row 0, row 1, and row 2. These rows are exactly the same as row R = 0.
Next, we consider row R = 0 and column C = 2:
- Rule 1: Row R = 0 and column C = 2 should both have exactly N number of "1's".
- However, this violates Rule 1 since row R = 0 has three "1's" and column C = 2 has only one "1".
- Total "1's" till now = 3 + 0
Finally, we consider row R = 0 and column C = 3:
- Rule 1: Row R = 0 and column C = 3 both have exactly N = 3 "1's".
- Rule 2: The rows that have "1's" at column C = 3 are row 0, row 1, and row 2. These rows are exactly the same as row R = 0.
- Total "1's" till now = 3 + 0 + 3 = 6
We can apply this process for all rows and columns to determine the total count of "1's" following the given rules.
Input: mat = [[0, 1, 0, 1, 1, 0], [0, 1, 0, 1, 1, 0], [0, 1, 0, 1, 1, 0], [0, 0, 1, 0, 0, 0]], N = 3
Output: 9