# [LeetCode]#1337. The K Weakest Rows in a Matrix

`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 = 3Output: [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]`
`Input: mat = [[1,0,0,0], [1,1,1,1], [1,0,0,0], [1,0,0,0]], k = 2Output: [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]`
1. Convert mat to a dictionary by summary each list[i] value.
2. Sort is and return
`class Solution:    def kWeakestRows(self, mat, k):        ans={}        for i in range(len(mat)):            ans[i]=sum(mat[i])        ans=sorted(ans, key=ans.get)        return ans[:k]`

--

--

## More from Fatboy Slim

Interesting in any computer science.

Love podcasts or audiobooks? Learn on the go with our new app.

## Fatboy Slim

101 Followers

Interesting in any computer science.