LeetCode 题解工作台
找到矩阵中的好子集
给你一个下标从 0 开始大小为 m x n 的二进制矩阵 grid 。 从原矩阵中选出若干行构成一个行的 非空 子集,如果子集中任何一列的和至多为子集大小的一半,那么我们称这个子集是 好子集 。 更正式的,如果选出来的行子集大小(即行的数量)为 k,那么每一列的和至多为 floor(k / 2) 。…
4
题型
6
代码语言
3
相关题
当前训练重点
困难 · 数组·哈希·扫描
答案摘要
我们可以从小到大考虑答案选择的行数 。 - 如果 $k = 1$,每一列的和最大为 ,那么必须满足有一行的所有元素都是 ,否则无法满足条件。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路
题目描述
给你一个下标从 0 开始大小为 m x n 的二进制矩阵 grid 。
从原矩阵中选出若干行构成一个行的 非空 子集,如果子集中任何一列的和至多为子集大小的一半,那么我们称这个子集是 好子集。
更正式的,如果选出来的行子集大小(即行的数量)为 k,那么每一列的和至多为 floor(k / 2) 。
请你返回一个整数数组,它包含好子集的行下标,请你将其 升序 返回。
如果有多个好子集,你可以返回任意一个。如果没有好子集,请你返回一个空数组。
一个矩阵 grid 的行 子集 ,是删除 grid 中某些(也可能不删除)行后,剩余行构成的元素集合。
示例 1:
输入:grid = [[0,1,1,0],[0,0,0,1],[1,1,1,1]] 输出:[0,1] 解释:我们可以选择第 0 和第 1 行构成一个好子集。 选出来的子集大小为 2 。 - 第 0 列的和为 0 + 0 = 0 ,小于等于子集大小的一半。 - 第 1 列的和为 1 + 0 = 1 ,小于等于子集大小的一半。 - 第 2 列的和为 1 + 0 = 1 ,小于等于子集大小的一半。 - 第 3 列的和为 0 + 1 = 1 ,小于等于子集大小的一半。
示例 2:
输入:grid = [[0]] 输出:[0] 解释:我们可以选择第 0 行构成一个好子集。 选出来的子集大小为 1 。 - 第 0 列的和为 0 ,小于等于子集大小的一半。
示例 3:
输入:grid = [[1,1,1],[1,1,1]] 输出:[] 解释:没有办法得到一个好子集。
提示:
m == grid.lengthn == grid[i].length1 <= m <= 1041 <= n <= 5grid[i][j]要么是0,要么是1。
解题思路
方法一:分情况讨论
我们可以从小到大考虑答案选择的行数 。
- 如果 ,每一列的和最大为 ,那么必须满足有一行的所有元素都是 ,否则无法满足条件。
- 如果 ,每一列的和最大为 ,那么必须存在有两行,且这两行的元素按位与之后的结果是 ,否则无法满足条件。
- 如果 ,每一列的和最大也是 。如果 不满足条件,那么 也一定不满足条件,所以我们不需要考虑所有 且 为奇数的情况。
- 如果 ,每一列的和最大为 ,此时一定是 不满足条件,也就是说,任意选取两行,都存在至少一个列的和为 。我们在 行中任意选取 行,一共有 种选法,那么存在至少 个 的列。由于列数 ,所以一定存在至少一列的和大于 ,所以 也不满足条件。
- 对于 且 为偶数的情况,我们可以得出同样的结论,即 一定不满足条件。
综上所述,我们只需要考虑 和 的情况即可。即判断是否有一行全为 ,或者是否存在两行按位与之后的结果为 。
时间复杂度 ,空间复杂度 。其中 和 分别是矩阵的行数和列数。
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 []
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Candidates should demonstrate knowledge of subset generation techniques, such as sliding windows or bit manipulation.
- question_mark
Look for candidates who propose optimizing the checking of column sums using hash tables or other data structures.
- question_mark
A good candidate will be able to reason about the trade-offs between time complexity and the constraints of the problem.
常见陷阱
外企场景- error
Overlooking the need to optimize subset generation and column sum calculations can result in inefficient solutions.
- error
Failing to handle edge cases, such as matrices with very small sizes or matrices where no valid subset exists.
- error
Not leveraging bit manipulation for small column sizes may lead to unnecessary complexity in generating subsets.
进阶变体
外企场景- arrow_right_alt
Consider a variant where the matrix is not binary, but contains other values that must be summed and checked.
- arrow_right_alt
A variant where the maximum size of the subset is restricted to a particular value could be used to challenge optimization skills.
- arrow_right_alt
A dynamic variant where rows are added or removed from the matrix, requiring the solution to be updated incrementally.