-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKth Smallest Element in a Sorted Matrix.py
49 lines (42 loc) · 1.33 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
'''
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.
Note that it is the kth smallest element in the sorted order, not the kth distinct element.
Example:
matrix = [
[ 1, 5, 9],
[10, 11, 13],
[12, 13, 15]
],
k = 8,
return 13.
Note:
You may assume k is always valid, 1 ≤ k ≤ n2.
'''
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def kthSmallest(self, matrix, k):
"""
:type matrix: List[List[int]]
:type k: int
:rtype: int
"""
visited = [[False for j in xrange(len(matrix[0]))] for i in xrange(len(matrix))]
h = []
heapq.heappush(h, (matrix[0][0], 0, 0))
visited[0][0] = True
for _ in xrange(k):
val, x, y = heapq.heappop(h)
if _ == k-1:
return val
if x+1 < len(matrix) and not visited[x+1][y]:
visited[x+1][y] = True
heapq.heappush(h, (matrix[x+1][y], x+1, y))
if y+1 < len(matrix[0]) and not visited[x][y+1]:
visited[x][y+1] = True
heapq.heappush(h, (matrix[x][y+1], x, y+1))
return -1