generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.cpp
36 lines (29 loc) · 929 Bytes
/
solution.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
#include <vector>
class Solution {
public:
std::vector<int> kthSmallestPrimeFraction(std::vector<int>& arr, int k) {
const int n = arr.size();
double left{0.0};
double right{1.0};
while (left <= right) {
const double mid{left + (right - left) / 2.0};
int total{0};
int den{1};
double maxFrac{0.0};
int selectedNum{0};
int selectedDen{0};
for (int num = 0; num < n; ++num) {
while (den < n && arr[num] >= arr[den] * mid) ++den;
total += n - den;
if (den < n && maxFrac < static_cast<double>(arr[num]) / static_cast<double>(arr[den])) {
maxFrac = static_cast<double>(arr[num]) / static_cast<double>(arr[den]);
selectedNum = num;
selectedDen = den;
}
}
if (total == k) return {arr[selectedNum], arr[selectedDen]};
total > k ? right = mid : left = mid;
}
return {0, 0};
}
};