-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathkth_smallest_in_multiplication.cpp
44 lines (32 loc) · 1.94 KB
/
kth_smallest_in_multiplication.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
/*
Why heap is inefficient here and how we come with a design function -
For Kth-Smallest problems like this, what comes to our mind first is Heap. Usually we can maintain a Min-Heap and just pop the top of the Heap for k times. However, that doesn't work out in this problem. We don't have every single number in the entire Multiplication Table, instead, we only have the height and the length of the table. If we are to apply Heap method, we need to explicitly calculate these m * n values and save them to a heap. The time complexity and space complexity of this process are both O(mn), which is quite inefficient. This is when binary search comes in. Remember we say that designing condition function is the most difficult part? In order to find the k-th smallest value in the table, we can design an enough function, given an input num, determine whether there're at least k values less than or equal to num. The minimal num satisfying enough function is the answer we're looking for. Recall that the key to binary search is discovering monotonicity. In this problem, if num satisfies enough, then of course any value larger than num can satisfy.
*/
class Solution {
//based on binary search template to solve all BS problems
bool isEnough(int m, int n, int k, int num) {
int count = 0;
for(int i = 1; i < m + 1; i++) {
int add =min (num/i, n);
if (add == 0)
break;
count += add;
}
return count >= k;
}
public:
int findKthNumber(int m, int n, int k) {
int left = 1, right = m * n;
while(left < right) {
//find mid
int mid = left + (right-left)/2;
//condition
if (isEnough(m, n, k, mid))
right = mid;
//else
else
left = mid + 1;
}
return left;
}
};