Given a m * n matrix mat of ones (representing soldiers) and zeros (representing civilians), return the indexes of the k weakest rows in the matrix ordered from the weakest to the strongest.
A row i is weaker than row j, if the number of soldiers in row i is less than the number of soldiers in row j, or they have the same number of soldiers but i is less than j. Soldiers are always stand in the frontier of a row, that is, always ones may appear first and then zeros.
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 number of soldiers for each row is:
row 0 -> 2
row 1 -> 4
row 2 -> 1
row 3 -> 2
row 4 -> 5
Rows ordered from the weakest to the strongest are [2,0,3,1,4]
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:
The number of soldiers for each row is:
row 0 -> 1
row 1 -> 4
row 2 -> 1
row 3 -> 1
Rows ordered from the weakest to the strongest are [0,2,3,1]
publicint[] kWeakestRows(int[][] mat,int k) {if (mat ==null||mat.length==0|| mat[0].length==0|| k <1|| k >mat.length) {returnnull; }// Pair<1's count, row num>PriorityQueue<Pair<Integer,Integer>> pq =newPriorityQueue<>(k, (p1, p2) -> {if (p1.getKey() ==p2.getKey()) {returnp1.getValue().compareTo(p2.getValue()); } else {returnp1.getKey().compareTo(p2.getKey()); } });// count ones then put in heapfor (int row =0; row <mat.length; row++) {int oneCnt =0;for (int col =0; col < mat[row].length; col++) {if (mat[row][col] ==1) { oneCnt++; } }Pair p =newPair(oneCnt, row);pq.offer(p); }int[] res =newint[k];// pull top kfor (int i =0; i <res.length; i++) { res[i] =pq.poll().getValue(); }return res;}// leetcode solution vertical 解法publicint[] kWeakestRows(int[][] mat,int k) {int m =mat.length;int n = mat[0].length;int [] indexes =newint[k];int nextInsertIndex =0;// This code does the same as the animation above.for (int c =0; c < n && nextInsertIndex < k; c++) {for (int r =0; r < m && nextInsertIndex < k; r++) {// If this is the first 0 in the current row.if (mat[r][c] ==0&& (c ==0|| mat[r][c -1] ==1)) { indexes[nextInsertIndex] = r; nextInsertIndex++; } } }/* If there aren't enough, it's because some of the first k weakest rows * are entirely 1's. We need to include the ones with the lowest indexes * until we have at least k. */for (int r =0; nextInsertIndex < k ; r++) {/* If index i in the last column is 1, this was a full row and therefore * couldn't have been included in the output yet. */if (mat[r][n -1] ==1) { indexes[nextInsertIndex] = r; nextInsertIndex++; } }return indexes;}