LeetCode 1337 The K Weakest Rows in a Matrix (Python)

Posted by 小明MaxMing on February 15, 2021

题目

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.

解题思路

先统计出来每一行有多少个士兵,然后连同行号一起放到堆里,pop出来前k个

代码

class Solution:
    def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
        power = []
        for r, row in enumerate(mat):
            tmp = 0
            for n in row:
                if n:
                    tmp += 1
                else:
                    break
            power.append((tmp, r))
        heapq.heapify(power)
        return [heapq.heappop(power)[1] for _ in range(k)]

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅