The K Weakest Rows in a Matrix - Problem

Imagine you're a military strategist analyzing the defensive strength of different battle formations. You have an m x n binary matrix representing a battlefield where 1's represent soldiers and 0's represent civilians.

The key insight is that soldiers always position themselves in front of civilians - meaning all 1's appear to the left of all 0's in each row.

Your mission: Identify the k weakest rows based on these rules:

  • A row is weaker if it has fewer soldiers
  • If two rows have the same number of soldiers, the one with the smaller index is weaker

Return the indices of the k weakest rows, ordered from weakest to strongest.

Example: If row 0 has 2 soldiers, row 1 has 1 soldier, and row 2 has 3 soldiers, and k=2, you'd return [1, 0] because row 1 is weakest (1 soldier), followed by row 0 (2 soldiers).

Input & Output

example_1.py — Basic Case
$ Input: mat = [[1,1,0,0,0],[1,1,1,1,0],[1,0,0,0,0],[1,1,0,0,0],[1,1,1,1,1]], k = 3
Output: [2,0,3]
💡 Note: Row 2 has 1 soldier (weakest), row 0 has 2 soldiers, row 3 has 2 soldiers (but comes after row 0), so the 3 weakest are [2,0,3]
example_2.py — Tie Breaking
$ Input: mat = [[1,0,0,0],[1,1,1,1],[1,0,0,0],[1,0,0,0]], k = 2
Output: [0,2]
💡 Note: Rows 0, 2, 3 all have 1 soldier each. Since we need k=2 weakest and there's a tie, we pick the ones with smaller indices: [0,2]
example_3.py — All Different
$ Input: mat = [[1,1,1],[1,1,0],[1,0,0]], k = 2
Output: [2,1]
💡 Note: Row 2 has 1 soldier (weakest), row 1 has 2 soldiers (second weakest), row 0 has 3 soldiers (strongest)

Constraints

  • m == mat.length
  • n == mat[i].length
  • 2 ≤ n, m ≤ 100
  • 1 ≤ k ≤ m
  • matrix[i][j] is either 0 or 1
  • All 1's appear before all 0's in each row (soldiers before civilians)

Visualization

Tap to expand
K Weakest Rows in a Matrix INPUT Binary Matrix (m x n) Row 0: 1 1 0 0 0 = 2 Row 1: 1 1 1 1 0 = 4 Row 2: 1 0 0 0 0 = 1 Row 3: 1 1 0 0 0 = 2 Row 4: 1 1 1 1 1 = 5 = Soldier (1) = Civilian (0) Input Values mat = [[1,1,0,0,0],...] k = 3 Soldier Count per Row [2, 4, 1, 2, 5] (Row 2 has fewest soldiers) ALGORITHM STEPS 1 Count Soldiers Use binary search to find count of 1s in each row 2 Create Pairs Store (count, index) for each row in array 3 Sort Pairs Sort by count, then by index (for ties) 4 Extract K Indices Return first k row indices from sorted list Sorted (count, index) pairs: (1, 2) Weakest (2, 0) (2, 3) (4, 1) (5, 4) Strongest FINAL RESULT K = 3 Weakest Rows 1st Weakest: Row 2 Only 1 soldier 2 2nd Weakest: Row 0 2 soldiers (lower index) 0 3rd Weakest: Row 3 2 soldiers (higher index) 3 Output Array 2 0 3 OK - Result: [2, 0, 3] Indices ordered weakest to strongest Key Insight: Since soldiers (1s) are always positioned to the LEFT of civilians (0s), we can use BINARY SEARCH to efficiently count soldiers in O(log n) per row. Combined with sorting, the overall complexity becomes O(m * log n + m * log m) instead of O(m * n) for naive counting approach. TutorialsPoint - The K Weakest Rows in a Matrix | Optimal Solution (Binary Search + Sorting)
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
58.0K Views
Medium Frequency
~15 min Avg. Time
2.1K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen