-
Notifications
You must be signed in to change notification settings - Fork 5
/
KthSmallestInMultiplicationTable.java
90 lines (73 loc) · 2.63 KB
/
KthSmallestInMultiplicationTable.java
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
/*https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/*/
/*Inefficient PQ implementation*/
class Data implements Comparable<Data>
{
int row, col;
int product;
Data(int r, int c)
{
row = r;
col = c;
product = row*col;
}
@Override
public int compareTo(Data d)
{
return this.product-d.product;
}
}
class Solution
{
public int findKthNumber(int m, int n, int k)
{
PriorityQueue<Data> minHeap = new PriorityQueue<Data>();
int result = -1;
for (int i = 1; i <= m; ++i)
minHeap.add(new Data(i,1));
while (k-- > 0)
{
Data curr = minHeap.poll();
if (curr.col < n)
minHeap.add(new Data(curr.row,curr.col+1));
result = curr.product;
}
return result;
}
}
/*Efficient Binary Search Solution*/
class Solution {
private int fun(int m, int n, int x, int k)
{
//count will be storing the total numbers that occur before x including x.
int count = 0;
//we will be checking each row.
for (int i = 1; i <= m; ++i)
{
//Obviously if count becomes greater than k then we will have to break beacause we will have to look for smaller candidate.
if(count > k) break;
//Simple maths : If out x is grater than the last element in that row so obviously no. of elements less than or equal to x wil be n or otherwise it will be x/i.
int ct = Math.min(n, x/i);
count += ct;
}
//at last we return the count.
return count;
}
public int findKthNumber(int m, int n, int k) {
int low, high;
//Min and Max possible will be 1 and m*n respectively.
low = 1;
high = m*n;
//kth position actually means that exactly k-1 elements occur before it.(obviously in sorted array) or we can say if we include the kth element also then exactly k elements occur before it.
//Simple Binary Searching from the possible candidates for the kth position.
while(low < high)
{
//mid can be a possible candidate.
int mid = (low+high)/2;
//fun function returns how many numbers are less than mid including mid.
//if it is greater than or equal then we check for a smaller candidate. Or else we check for bigger candidate.
if (fun(m, n, mid, k) >= k) high = mid;
else low = mid+1;
}
return high;
}
}