Approach
Depth-first search
For Maximum Rows Covered by Columns, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.
- Define the state carried into one recursive or stack frame.
- Mark or choose the current state before exploring children.
- Combine child results or undo the choice when the branch finishes.
Code notes
- 33 lines of Python from the credited upstream file 2397.py.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected, together with recursive traversal.
Complexity
Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution:2 def maximumRows(self, matrix: list[list[int]], numSelect: int) -> int:3 ans = 04 5 def dfs(colIndex: int, leftColsCount: int, mask: int):6 nonlocal ans7 if leftColsCount == 0:8 ans = max(ans, self._getAllZerosRowCount(matrix, mask))9 return10 11 if colIndex == len(matrix[0]):12 return13 14 15 dfs(colIndex + 1, leftColsCount - 1, mask | 1 << colIndex)16 17 dfs(colIndex + 1, leftColsCount, mask)18 19 dfs(0, numSelect, 0)20 return ans21 22 def _getAllZerosRowCount(self, matrix: list[list[int]], mask: int) -> int:23 count = 024 for row in matrix:25 isAllZeros = True26 for i, num in enumerate(row):27 if num == 1 and (mask >> i & 1) == 0:28 isAllZeros = False29 break30 if isAllZeros:31 count += 132 return count33