Find a Good Subset of the Matrix Problem
Description
LeetCode Problem 2732.
You are given a 0-indexed m x n binary matrix grid.
Let us call a non-empty subset of rows good if the sum of each column of the subset is at most half of the length of the subset.
More formally, if the length of the chosen subset of rows is k, then the sum of each column should be at most floor(k / 2).
Return an integer array that contains row indices of a good subset sorted in ascending order.
If there are multiple good subsets, you can return any of them. If there are no good subsets, return an empty array.
A subset of rows of the matrix grid is any matrix that can be obtained by deleting some (possibly none or all) rows from grid.
Example 1:
Input: grid = [[0,1,1,0],[0,0,0,1],[1,1,1,1]]
Output: [0,1]
Explanation: We can choose the 0^th and 1^st rows to create a good subset of rows.
The length of the chosen subset is 2.
- The sum of the 0^th column is 0 + 0 = 0, which is at most half of the length of the subset.
- The sum of the 1^st column is 1 + 0 = 1, which is at most half of the length of the subset.
- The sum of the 2^nd column is 1 + 0 = 1, which is at most half of the length of the subset.
- The sum of the 3^rd column is 0 + 1 = 1, which is at most half of the length of the subset.
Example 2:
Input: grid = [[0]]
Output: [0]
Explanation: We can choose the 0^th row to create a good subset of rows.
The length of the chosen subset is 1.
- The sum of the 0^th column is 0, which is at most half of the length of the subset.
Example 3:
Input: grid = [[1,1,1],[1,1,1]]
Output: []
Explanation: It is impossible to choose any subset of rows to create a good subset.
Constraints:
m == grid.lengthn == grid[i].length1 <= m <= 10^41 <= n <= 5grid[i][j]is either0or1.
Solution
Approach: Only Subsets of Size 1 or 2 Matter
A subset of k rows is good when every column sum is at most floor(k / 2). Check the sizes one by one.
k = 1: every column sum must be0, so we need a row made only of zeros.k = 2: every column sum must be at most1, so we need two rows that have no1in the same column, i.e. the bitwise AND of their masks is0.k = 3: the limit is again1. If three rows satisfied it, any two of them would already satisfy it, so a subset of size 2 would exist.k = 4: the limit is2. Suppose no two rows are compatible. Then each of theC(4, 2) = 6pairs shares a column holding1s. A column wherecof the rows have a1accounts forC(c, 2)pairs, andc ≤ 2gives at most one pair per column, so at least 6 columns would be needed. There are at most 5, so some column has a sum above2.- Larger
kis handled the same way: whenever a larger subset is good, a pair of its rows is good too.
So it is enough to look for an all-zero row, or for two rows with disjoint masks. Encode each row as a bitmask (at most 5 bits), keep one row index per distinct mask, and test every pair of distinct masks (at most 32 × 32 of them). Return the two indices in ascending order.
Complexity: O(m · n + 4n) time and O(2n) space.
class Solution:
def goodSubsetofBinaryMatrix(self, grid: List[List[int]]) -> List[int]:
g = {}
for i, row in enumerate(grid):
mask = 0
for j, x in enumerate(row):
mask |= x << j
if mask == 0:
return [i]
g[mask] = i
for a, i in g.items():
for b, j in g.items():
if (a & b) == 0:
return sorted([i, j])
return []