forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows.py
67 lines (59 loc) · 1.95 KB
/
find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows.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
# Time: O(m * klogk)
# Space: O(k)
import heapq
class Solution(object):
def kthSmallest(self, mat, k):
"""
:type mat: List[List[int]]
:type k: int
:rtype: int
"""
def kSmallestPairs(nums1, nums2, k):
result, min_heap = [], []
for c in xrange(min(len(nums1), k)):
heapq.heappush(min_heap, (nums1[c]+nums2[0], 0))
c += 1
while len(result) != k and min_heap:
total, c = heapq.heappop(min_heap)
result.append(total)
if c+1 == len(nums2):
continue
heapq.heappush(min_heap, (total-nums2[c]+nums2[c+1], c+1))
return result
result = mat[0]
for r in xrange(1, len(mat)):
result = kSmallestPairs(result, mat[r], k)
return result[k-1]
# Time: O((k + m) * log(m * MAX_NUM)) ~ O(k * m * log(m * MAX_NUM))
# Space: O(m)
class Solution2(object):
def kthSmallest(self, mat, k):
"""
:type mat: List[List[int]]
:type k: int
:rtype: int
"""
def countArraysHaveSumLessOrEqual(mat, k, r, target): # Time: O(k + m) ~ O(k * m)
if target < 0:
return 0
if r == len(mat):
return 1
result = 0
for c in xrange(len(mat[0])):
cnt = countArraysHaveSumLessOrEqual(mat, k-result, r+1, target-mat[r][c])
if not cnt:
break
result += cnt
if result > k:
break
return result
MAX_NUM = 5000
left, right = len(mat), len(mat)*MAX_NUM
while left <= right:
mid = left + (right-left)//2
cnt = countArraysHaveSumLessOrEqual(mat, k, 0, mid)
if cnt >= k:
right = mid-1
else:
left = mid+1
return left