Problem
Ask
Submissions

Problem: The K Weakest Rows in a Matrix

Easy
15 min
Understand how to apply modified binary search to determine the k weakest rows in a binary matrix with rows of soldiers and civilians. Learn to compare row strengths based on soldier counts and order indices, and practice solving this common coding interview problem.

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

Easy
15 min
Understand how to apply modified binary search to determine the k weakest rows in a binary matrix with rows of soldiers and civilians. Learn to compare row strengths based on soldier counts and order indices, and practice solving this common coding interview problem.

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.