-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetCode.cpp
39 lines (33 loc) · 1002 Bytes
/
leetCode.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
class Solution {
public:
vector<int> kthSmallestPrimeFraction(vector<int>& arr, int k) {
int n = arr.size();
double left = 0, right = 1, mid;
vector<int> res;
while (left <= right) {
mid = left + (right - left) / 2;
int j = 1, total = 0, num = 0, den = 0;
double maxFrac = 0;
for (int i = 0; i < n; ++ i) {
while (j < n && arr[i] > arr[j] * mid) {
++j;
}
total += n - j;
if (j < n && maxFrac < arr[i] * 1.0 / arr[j]) {
maxFrac = arr[i] * 1.0 / arr[j];
num = i; den = j;
}
}
if (total == k) {
res = {arr[num], arr[den]};
break;
}
if (total > k) {
right = mid;
} else {
left = mid;
}
}
return res;
}
};