Problem
Ask
Submissions

Problem: The K Weakest Rows in a Matrix

Medium
30 min
Explore how to find the k weakest rows in a binary matrix where soldiers are represented by ones and ordered before civilians. Understand how to apply modified binary search to count soldiers efficiently, compare rows, and return the weakest in sorted order to solve this coding interview pattern.

Statement

You are given an m×nm \times n binary matrix of 11’s (representing soldiers) and 00’s (representing civilians). The soldiers are positioned in front of the civilians, i.e., all the 11’s will appear to the left of all the 00’s in each row.

A row ii is weaker than a row jj if any of the following is TRUE:

  • The number of soldiers in row ii is less than the number of soldiers in row jj.

  • Both rows have the same number of soldiers and i<ji \lt j.

You have to return the indexes of the kk weakest rows in the matrix ordered from weakest to strongest.

Constraints:

  • m=m = matrix.length

  • n=n = matrix[i].length

  • 2n,m1002 \leq n, m \leq 100

  • 1km1 \leq k \leq m

  • matrix[i][j] is either 00 or 11.

Problem
Ask
Submissions

Problem: The K Weakest Rows in a Matrix

Medium
30 min
Explore how to find the k weakest rows in a binary matrix where soldiers are represented by ones and ordered before civilians. Understand how to apply modified binary search to count soldiers efficiently, compare rows, and return the weakest in sorted order to solve this coding interview pattern.

Statement

You are given an m×nm \times n binary matrix of 11’s (representing soldiers) and 00’s (representing civilians). The soldiers are positioned in front of the civilians, i.e., all the 11’s will appear to the left of all the 00’s in each row.

A row ii is weaker than a row jj if any of the following is TRUE:

  • The number of soldiers in row ii is less than the number of soldiers in row jj.

  • Both rows have the same number of soldiers and i<ji \lt j.

You have to return the indexes of the kk weakest rows in the matrix ordered from weakest to strongest.

Constraints:

  • m=m = matrix.length

  • n=n = matrix[i].length

  • 2n,m1002 \leq n, m \leq 100

  • 1km1 \leq k \leq m

  • matrix[i][j] is either 00 or 11.