forked from pxu/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKthSmallestPrimeFraction.java
More file actions
61 lines (56 loc) · 1.79 KB
/
KthSmallestPrimeFraction.java
File metadata and controls
61 lines (56 loc) · 1.79 KB
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
// leetcode 786
public class KthSmallestPrimeFraction {
private static final double err = 1e-9;
private int smaller(double [] A, double val) {
int cnt = 0;
// fix q as A[j], binary search p as A[i]
for (int j = 1; j < A.length; ++j) {
// find the last element that is smaller than val
int lo = 0;
int hi = j - 1;
while (lo < hi) {
int mid = (lo + hi + 1) / 2;
if (A[mid] / A[j] < val) {
lo = mid;
} else {
hi = mid - 1;
}
}
if (lo == hi) {
cnt += ((A[lo] / A[j] < val) ? lo + 1 : lo);
}
}
return cnt;
}
public int[] kthSmallestPrimeFraction(int[] AA, int K) {
// binary search the value
double [] A = new double [AA.length];
Set<Integer> nums = new HashSet<>();
for (int i = 0; i < A.length; ++i) {
A[i] = AA[i];
nums.add(AA[i]);
}
double lo = 1.0 / 30000;
double hi = 29999.0 / 30000;
// time Complexity: O(log(10^9)) because err = 1e-9
while (hi - lo > err) {
double mid = (lo + hi) / 2;
int cnt = smaller(A, mid); // how many fractions smaller than mid
if (cnt > K - 1) {
hi = mid;
} else {
lo = mid;
}
}
for (int i = 0; i < A.length; ++i) {
// search for each possible q (A[i])
int p = (int)Math.round(lo * A[i]);
if (p < A[i] && nums.contains(p) && Math.abs(p/A[i] - lo) < err) {
return new int[] {p, AA[i]};
}
}
// unreachable
assert(false);
return null;
}
}