The K Weakest Rows in a Matrix

Try to solve The K Weakest Rows in a Matrix 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

  • 2≤n,m≤1002 \leq n, m \leq 100

  • 1≤k≤m1 \leq k \leq m

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

Examples

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.