← All problems

The K Weakest Rows in a Matrix

Easy heapssortingmatrix LeetCode ↗
You are given an m x n binary matrix where each row is sorted (all 1s come before all 0s). A row's strength is its count of 1s; a row is weaker than another if it has fewer 1s, breaking ties by smaller row index. Return the indices of the k weakest rows, ordered from weakest to strongest.

Hints (0/3 revealed)

Log in to reveal hints progressively.

Complexity

Time O(m*n + m log m), space O(m).

Common pitfalls

  • Breaking ties incorrectly — weaker means smaller index on equal counts.
  • Counting 1s with a linear scan per row when binary search is available (minor).
  • Returning strengths instead of row indices.

Your solution

Log in to save your code and track progress.