You are given an m x n binary matrix mat of 1's (representing soldiers) and 0's (representing civilians). The soldiers are positioned in front of the civilians. That is, all the 1's will appear to the left of all the 0's in each row.

A row i is weaker than a row j if one of the following is true:


	The number of soldiers in row i is less than the number of soldiers in row j.
	Both rows have the same number of soldiers and i &lt; j.


Return the indices of the k weakest rows in the matrix ordered from weakest to strongest.

&nbsp;
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 in each row is: 
- Row 0: 2 
- Row 1: 4 
- Row 2: 1 
- Row 3: 2 
- Row 4: 5 
The rows ordered from weakest to 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 in each row is: 
- Row 0: 1 
- Row 1: 4 
- Row 2: 1 
- Row 3: 1 
The rows ordered from weakest to strongest are [0,2,3,1].


&nbsp;
Constraints:


	m == mat.length
	n == mat[i].length
	2 &lt;= n, m &lt;= 100
	1 &lt;= k &lt;= m
	matrix[i][j] is either 0 or 1.


class Solution:
    def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:

        row = []
        for i in range(len(mat)):
            row.append((sum(mat[i]), i))

        row.sort()
        ans = [idx for (val, idx) in row[:k]]

        return ans
class Solution {
    public int[] kWeakestRows(int[][] mat, int k) {
        Map<Integer, Integer> map = new HashMap<>();
        List<Integer> list = new ArrayList<>();
        int[] arr = new int[k];
        for (int i = 0; i < mat.length; i++){
            int n = getBits(mat[i]);
            map.put(i, n);
            list.add(n);
        }
        Collections.sort(list);
        int z = 0;
        for (int i = 0; i < k; i++){
            for (Map.Entry<Integer, Integer> m : map.entrySet()){
                if (list.get(i).equals(m.getValue())){
                    arr[z++] = m.getKey();
                    map.remove(m.getKey(), m.getValue());
                    break;
                }
            }
        }

        return arr;
    }

    private static Integer getBits(int[] arr) {
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == 1) count++;
        }

        return count;
    }
}
class Solution {
public:
    vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {
        // <idx, count>
        vector<pair<int,int>> freqMapper;
        int civilian = 0;
        for(int i=0; i<mat.size(); ++i) {
            int degree = count(mat[i].begin(), mat[i].end(), civilian);
            freqMapper.push_back({i, degree});
        }
        sort(freqMapper.begin(), freqMapper.end(), [](pair<int,int> pair1, pair<int,int> pair2) {
        if (pair1.second > pair2.second) {
            return true;
        } else if (pair1.second == pair2.second) {
            return pair1.first < pair2.first;
        }
        return pair1.second > pair2.second;
    });
        vector<int> kWeakest;
        for(int i=0; i<k; i++) kWeakest.push_back(freqMapper[i].first);
        return kWeakest;
    }
    
};
/**
 * @param {number[][]} mat
 * @param {number} k
 * @return {number[]}

 * S: O(N)
 * T: O(N*logN)
 */
var kWeakestRows = function(mat, k) {
    return mat.reduce((acc, row, index) => {
        let left = 0;
        let right = row.length - 1;

        while(left <= right) {
            let mid = Math.floor( (left + right) / 2);

            if(row[mid]) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }

        acc.push({ index, value: left });

        return acc;
    }, []).sort((a, b) => a.value - b.value).splice(0, k).map(item => item.index);
};
The K Weakest Rows in a Matrix