Problem
Ask
Submissions

Problem: The K Weakest Rows in a Matrix

Medium
30 min
Understand how to determine the k weakest rows in a binary matrix by comparing the number of soldiers in each row. Explore the use of modified binary search to efficiently solve this problem, honing your skills in analyzing and implementing search adaptations for coding interviews.

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
Understand how to determine the k weakest rows in a binary matrix by comparing the number of soldiers in each row. Explore the use of modified binary search to efficiently solve this problem, honing your skills in analyzing and implementing search adaptations for coding interviews.

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.