Loading...
You are given an m x n binary matrix mat in which every row consists of some number of 1s followed by some number of 0s: within a row, no 1 ever appears after a 0.
Order the rows from fewest 1s to most, breaking ties by smaller row index first.
Return the indices of the first k rows in that order.
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]
Explanation: The rows hold 2, 4, 1, 2 and 5 ones. Row 2 has the fewest with 1. Rows 0 and 3 both have 2, so the smaller index comes first.
Input: mat = [[1,0,0,0],[1,1,1,1],[1,0,0,0],[1,0,0,0]], k = 2
Output: [0,2]
Explanation: Rows 0, 2 and 3 all hold 1 one and row 1 holds 4. The tie is broken by index, so rows 0 and 2 are returned.
m = mat.length, n = mat[i].lengthn, m ≤100mmat[i][j] is either 0 or 1Click "Run" to test with sample cases or "Submit" to run all tests.