-
Notifications
You must be signed in to change notification settings - Fork 257
/
kth-smallest-number-in-sorted-matrix.cpp
193 lines (168 loc) · 6.2 KB
/
kth-smallest-number-in-sorted-matrix.cpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Time: O(klog(min(m, n, k))
// Space: O(min(m, n, k))
class Solution {
public:
/**
* @param matrix: a matrix of integers
* @param k: an integer
* @return: the kth smallest number in the matrix
*/
int kthSmallest(vector<vector<int>> &matrix, int k) {
int kth_smallest = 0;
using P = pair<int, pair<int, int>>;
priority_queue<P, vector<P>, greater<P>> q;
auto push = [&matrix, &q](int i, int j) {
if (matrix.size() > matrix[0].size()) {
if (i < matrix[0].size() && j < matrix.size()) {
q.emplace(matrix[j][i], make_pair(i, j));
}
} else {
if (i < matrix.size() && j < matrix[0].size()) {
q.emplace(matrix[i][j], make_pair(i, j));
}
}
};
push(0, 0);
while (!q.empty() && k--) {
auto tmp = q.top(); q.pop();
kth_smallest = tmp.first;
int i, j;
tie(i, j) = tmp.second;
push(i, j + 1);
if (j == 0) {
push(i + 1, 0);
}
}
return kth_smallest;
}
};
// BST solution.
class Solution2 {
public:
/**
* @param matrix: a matrix of integers
* @param k: an integer
* @return: the kth smallest number in the matrix
*/
int kthSmallest(vector<vector<int>> &matrix, int k) {
if (matrix.size() < matrix[0].size()) { // Height is smaller.
return horizontal_search(matrix, k);
} else { // Width is smaller.
return vertical_search(matrix, k);
}
}
int horizontal_search(const vector<vector<int>> &matrix, int k) {
multimap<int, pair<int, int>> min_bst;
// Init BST by the first element of the first kth row.
for (int i = 0; i < min(static_cast<int>(matrix.size()), k); ++i) {
min_bst.emplace(pair<int, pair<int, int>>{matrix[i][0], {i, 0}});
}
int kth_smallest = INT_MAX;
while (!min_bst.empty() && k--) {
// Pop the min of BST.
if (k == 0) {
kth_smallest = min_bst.cbegin()->first;
}
// Pop the min of BST.
int i = min_bst.cbegin()->second.first;
int j = min_bst.cbegin()->second.second;
min_bst.erase(min_bst.cbegin());
// Insert the next possible element.
if (j + 1 < matrix[i].size()) {
min_bst.emplace(pair<int, pair<int, int>>{matrix[i][j + 1], {i, j + 1}});
}
}
return kth_smallest;
}
int vertical_search(const vector<vector<int>> &matrix, int k) {
multimap<int, pair<int, int>> min_bst;
// Init BST by the first element of the first kth column.
for (int j = 0; j < min(static_cast<int>(matrix[0].size()), k); ++j) {
min_bst.emplace(pair<int, pair<int, int>>{matrix[0][j], {0, j}});
}
int kth_smallest = INT_MAX;
while (!min_bst.empty() && k--) {
// Pop the min of Heap.
if (k == 0) {
kth_smallest = min_bst.cbegin()->first;
}
// Pop the min of BST.
int i = min_bst.cbegin()->second.first;
int j = min_bst.cbegin()->second.second;
min_bst.erase(min_bst.cbegin());
// Insert the next possible element.
if (i + 1 < matrix.size()) {
min_bst.emplace(pair<int, pair<int, int>>{matrix[i + 1][j], {i + 1, j}});
}
}
return kth_smallest;
}
};
// Time: O(klog(min(m, n, k))
// Space: O(min(m, n, k))
// Heap solution.
class Solution3 {
public:
struct Compare {
bool operator()(const pair<int, pair<int, int>>& a, const pair<int, pair<int, int>>& b) {
return a.first > b.first;
}
};
/**
* @param matrix: a matrix of integers
* @param k: an integer
* @return: the kth smallest number in the matrix
*/
int kthSmallest(vector<vector<int>> &matrix, int k) {
if (matrix.size() < matrix[0].size()) { // Height is smaller.
return horizontal_search(matrix, k);
} else { // Width is smaller.
return vertical_search(matrix, k);
}
}
int horizontal_search(const vector<vector<int>> &matrix, int k) {
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, Compare> min_heap;
// Init Heap by the first element of the first kth row.
for (int i = 0; i < min(static_cast<int>(matrix.size()), k); ++i) {
min_heap.emplace(pair<int, pair<int, int>>{matrix[i][0], {i, 0}});
}
int kth_smallest = INT_MAX;
while (!min_heap.empty() && k--) {
// Pop the min of Heap.
if (k == 0) {
kth_smallest = min_heap.top().first;
}
kth_smallest = min_heap.top().first;
int i = min_heap.top().second.first;
int j = min_heap.top().second.second;
min_heap.pop();
// Insert the next possible element.
if (j + 1 < matrix[i].size()) {
min_heap.emplace(pair<int, pair<int, int>>{matrix[i][j + 1], {i, j + 1}});
}
}
return kth_smallest;
}
int vertical_search(const vector<vector<int>> &matrix, int k) {
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, Compare> min_heap;
// Init Heap by the first element of the first kth column.
for (int j = 0; j < min(static_cast<int>(matrix[0].size()), k); ++j) {
min_heap.emplace(pair<int, pair<int, int>>{matrix[0][j], {0, j}});
}
int kth_smallest = INT_MAX;
while (!min_heap.empty() && k--) {
// Pop the min of Heap.
if (k == 0) {
kth_smallest = min_heap.top().first;
}
int i = min_heap.top().second.first;
int j = min_heap.top().second.second;
min_heap.pop();
// Insert the next possible element.
if (i + 1 < matrix.size()) {
min_heap.emplace(pair<int, pair<int, int>>{matrix[i + 1][j], {i + 1, j}});
}
}
return kth_smallest;
}
};