-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path1337.the-k-weakest-rows-in-a-matrix.py
48 lines (45 loc) · 1.41 KB
/
1337.the-k-weakest-rows-in-a-matrix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#
# @lc app=leetcode id=1337 lang=python3
#
# [1337] The K Weakest Rows in a Matrix
#
# @lc code=start
# TAGS: Heap, Array, Binary Search
class Solution:
# 108 ms, 100%. Time: O(MlogMlogN). Space: O(M)
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
def binary_search(row):
lo, hi = 0, len(row)
while lo < hi:
mid = (lo + hi)//2
if row[mid]:
lo = mid + 1
else:
hi = mid
return lo
temp = sorted([(binary_search(row), i) for i, row in enumerate(mat)])
return [i for v, i in temp][:k]
# Time: O(MlogNlogK). Space: O(K)
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
def binary_search(row):
lo, hi = 0, len(row)
while lo < hi:
mid = (lo + hi)//2
if row[mid]:
lo = mid + 1
else:
hi = mid
return lo
heap = []
for i, row in enumerate(mat):
strength = binary_search(row)
if len(heap) < k:
heapq.heappush(heap, (-strength, -i))
else:
heapq.heappushpop(heap, (-strength, -i))
rv = []
while heap:
strength, i = heapq.heappop(heap)
rv.append(-i)
return reversed(rv)
# @lc code=end