-
Notifications
You must be signed in to change notification settings - Fork 43
/
kth-smallest-element-in-a-sorted-matrix.py
126 lines (117 loc) · 3.67 KB
/
kth-smallest-element-in-a-sorted-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
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
118
119
120
121
122
123
124
125
126
# V0
# V1
# http://bookshadow.com/weblog/2016/08/01/leetcode-kth-smallest-element-in-a-sorted-matrix/
class Solution(object):
def kthSmallest(self, matrix, k):
"""
:type matrix: List[List[int]]
:type k: int
:rtype: int
"""
m, n = len(matrix), len(matrix[0])
visited = [[False] * n for _ in range(m)]
q = [(matrix[0][0], 0, 0)]
ans = None
visited[0][0] = True
for _ in range(k):
ans, i, j = heapq.heappop(q)
if i + 1 < m and not visited[i + 1][j]:
visited[i + 1][j] = True
heapq.heappush(q, (matrix[i + 1][j], i + 1, j))
if j + 1 < n and not visited[i][j + 1]:
visited[i][j + 1] = True
heapq.heappush(q, (matrix[i][j + 1], i, j + 1))
return ans
# V1'
# http://bookshadow.com/weblog/2016/08/01/leetcode-kth-smallest-element-in-a-sorted-matrix/
class Solution(object):
def kthSmallest(self, matrix, k):
"""
:type matrix: List[List[int]]
:type k: int
:rtype: int
"""
m, n = len(matrix), len(matrix[0])
q = [(matrix[0][0], 0, 0)]
ans = None
for _ in range(k):
ans, i, j = heapq.heappop(q)
if j == 0 and i + 1 < m:
heapq.heappush(q, (matrix[i + 1][j], i + 1, j))
if j + 1 < n:
heapq.heappush(q, (matrix[i][j + 1], i, j + 1))
return ans
# V1''
# http://bookshadow.com/weblog/2016/08/01/leetcode-kth-smallest-element-in-a-sorted-matrix/
class Solution(object):
def kthSmallest(self, matrix, k):
"""
:type matrix: List[List[int]]
:type k: int
:rtype: int
"""
lo, hi = matrix[0][0], matrix[-1][-1]
while lo <= hi:
mid = (lo + hi) >> 1
loc = sum(bisect.bisect_right(m, mid) for m in matrix)
if loc >= k:
hi = mid - 1
else:
lo = mid + 1
return lo
# V1'''
# http://bookshadow.com/weblog/2016/08/01/leetcode-kth-smallest-element-in-a-sorted-matrix/
class Solution(object):
def kthSmallest(self, matrix, k):
"""
:type matrix: List[List[int]]
:type k: int
:rtype: int
"""
lo, hi = matrix[0][0], matrix[-1][-1]
while lo <= hi:
mid = (lo + hi) >> 1
loc = self.countLower(matrix, mid)
if loc >= k:
hi = mid - 1
else:
lo = mid + 1
return lo
def countLower(self, matrix, num):
i, j = len(matrix) - 1, 0
cnt = 0
while i >= 0 and j < len(matrix[0]):
if matrix[i][j] <= num:
cnt += i + 1
j += 1
else:
i -= 1
return cnt
# V2
# Time: O(k * log(min(n, m, k))), with n x m matrix
# Space: O(min(n, m, k))
from heapq import heappush, heappop
class Solution(object):
def kthSmallest(self, matrix, k):
"""
:type matrix: List[List[int]]
:type k: int
:rtype: int
"""
kth_smallest = 0
min_heap = []
def push(i, j):
if len(matrix) > len(matrix[0]):
if i < len(matrix[0]) and j < len(matrix):
heappush(min_heap, [matrix[j][i], i, j])
else:
if i < len(matrix) and j < len(matrix[0]):
heappush(min_heap, [matrix[i][j], i, j])
push(0, 0)
while min_heap and k > 0:
kth_smallest, i, j = heappop(min_heap)
push(i, j + 1)
if j == 0:
push(i + 1, 0)
k -= 1
return kth_smallest