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.
Examples
Example 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]
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.
Example 2
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.
Constraints
m=mat.length, n=mat[i].length
2≤n, m≤100
1≤k≤m
mat[i][j] is either 0 or 1
Within each row, every 1 appears before every 0
Loading editor...
Log in to run your code. Your draft is saved in this browser.Log inSign up