The K Weakest Rows in a Matrix Problem
The K Weakest Rows in a Matrix Problem — ExecCode Easy DSA Practice
Solve the The K Weakest Rows in a Matrix problem on ExecCode. Free online easy DSA practice in Arrays - Logic Building. Write and run code in Java, C++, Python — no signup required to run.
Problem description
You are given an m x n binary matrix mat of 1's (soldiers) and 0's (civilians), where in every row all soldiers stand before all civilians. A row is weaker than another if it has fewer soldiers, or the same number but a smaller row index. Return the indices of the k weakest rows, ordered from weakest to strongest.
Examples
Input mat = [[1, 1, 0], [1, 0, 0], [0, 0, 0]], k = 2; Output [2, 1]. 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]. Input mat = [[1, 0, 0], [1, 1, 1], [1, 0, 0]], k = 1; Output [0]
Constraints
m == mat.length n == mat[i].length 2 ≤ n, m ≤ 100 1 ≤ k ≤ m mat[i][j] is either 0 or 1 Each row of mat is sorted in non-increasing order
Practice The K Weakest Rows in a Matrix free on ExecCode. Browse DSA problems, topic map, and placement guides.