1337. 矩阵中战斗力最弱的 K 行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
m = len(mat)-1
n = len(mat[0])-1
list_ = []
for i in range(m+1):
soldier_num = 0
for j in range(n+1):
if mat[i][j]==1:
soldier_num += 1
list_.append((i,soldier_num))
list_.sort(key=lambda x:x[1])
result = []
for ii in list_[:ii]:
result.append(k[0])
return result