Skip to content

Practice Find a Good Subset of the Matrix

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#2732
LeetCode Problem

Find a Good Subset of the Matrix

Problem Statement

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.

Source: Find a Good Subset of the Matrix on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
grid = [[0,1,1,0],[0,0,0,1],[1,1,1,1]]
Output
[0,1]

# Example 2

Input
grid = [[0]]
Output
[0]

# Example 3

Input
grid = [[1,1,1],[1,1,1]]
Output
[]

Constraints

m == grid.length
n == grid[i].length
1 <= m <= 10^4
1 <= n <= 5
grid[i][j] is either 0 or 1.

Related LeetCode Problems

Practice more problems similar to Find a Good Subset of the Matrix.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.