-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1337.py
117 lines (97 loc) · 3.04 KB
/
1337.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# [ LeetCode ] 1337. The K Weakest Rows in a Matrix
def solution(mat: list[list[int]], k: int) -> list[int]:
rows: dict[int, int] = {}
for index, row in enumerate(mat):
start, end = 0, len(row) - 1
while start <= end:
middle: int = start + (end - start) // 2
if row[middle] == 0:
end = middle - 1
else:
start = middle + 1
rows[index] = start
answer: list[int] = []
for row, _ in sorted(rows.items(), key=lambda x: (x[1], x[0])):
if k >= 1:
answer.append(row)
k -= 1
else:
break
return answer
def another_solution(mat: list[list[int]], k: int) -> list[int]:
import heapq
n: int = len(mat[0])
queue: list[tuple[int, int]] = []
for index in range(len(mat)):
start, end = 0, n - 1
while start <= end:
middle: int = start + (end - start) // 2
if mat[index][middle] == 0:
end = middle - 1
else:
start = middle + 1
entry: tuple[int, int] = (-start, -index)
if len(queue) < k or queue[0] < entry:
heapq.heappush(queue, entry)
if len(queue) > k:
heapq.heappop(queue)
answer: list[int] = []
while queue:
_, index = heapq.heappop(queue)
answer.append(-index)
answer.reverse()
return answer
def vertical_interation(mat: list[list[int]], k: int) -> list[int]:
answer: list[int] = []
m, n = len(mat), len(mat[0])
for column in len(n):
for row in range(m):
if (
len(answer) < k
and
mat[row][column] == 0
and (
column == 0
or
mat[row][column - 1] == 1
)
):
answer.append(row)
row: int = 0
while len(answer) < k:
if mat[row][-1] == 1:
answer.append(row)
row += 1
return answer
if __name__ == "__main__":
cases: list[dict[str, dict[str, list[list[int]] | int] | list[int]]] = [
{
"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]
},
{
"input": {
"mat": [
[1, 0, 0, 0],
[1, 1, 1, 1],
[1, 0, 0, 0],
[1, 0, 0, 0],
],
"k": 2
},
"output": [0, 2]
}
]
for case in cases:
assert case["output"] == solution(**case["input"])
assert case["output"] == another_solution(**case["input"])
# assert case["output"] == vertical_interation(**case["input"])