The K Weakest Rows in a Matrix
EasyHeap / Priority Queue
Decode it first. Clues, constraints, pattern and plan before any code.A grid mat describes an army: 1 is a soldier and 0 is a civilian. In every row, all the soldiers stand before all the civilians (the 1s come first).
Row i is weaker than row j if it has fewer soldiers, or the same number of soldiers and i < j. Return the indices of the k weakest rows, weakest first.
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 → [2, 0, 3]Soldier counts are 2, 4, 1, 2, 5. Row 2 is weakest, then rows 0 and 3 (tied at 2, so the smaller index first).
Constraints
2 <= m, n <= 100(m rows, n columns)1 <= k <= mmat[i][j]is0or1.