-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0786.KthSmallestPrimeFraction.cpp
More file actions
49 lines (41 loc) · 1.27 KB
/
0786.KthSmallestPrimeFraction.cpp
File metadata and controls
49 lines (41 loc) · 1.27 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
class Solution {
public:
vector<int>* arrValues;
set<pair<int, int>> usedIndices;
map<double, pair<int, int>> activeIndices;
int n;
inline void addActiveIndex(const pair<int, int>& indices) {
if (indices.first + indices.second >= n) return;
// Ignore if indices already used.
if (usedIndices.find(indices) != usedIndices.end()) return;
// Add to indices.
activeIndices.emplace((double)(*arrValues)[indices.first] / (double)(*arrValues)[n - indices.second], indices);
usedIndices.emplace(indices);
}
vector<int> kthSmallestPrimeFraction(vector<int>& arr, int k) {
// Initialize malarkey.
n = arr.size() - 1;
arrValues = &arr;
usedIndices = set<pair<int, int>>();
activeIndices = map<double, pair<int, int>>();
addActiveIndex(pair<int, int>(0, 0));
while (!activeIndices.empty() && k > 1) {
auto it = activeIndices.begin();
addActiveIndex(pair<int, int>(it->second.first + 1, it->second.second));
addActiveIndex(pair<int, int>(it->second.first, it->second.second + 1));
activeIndices.erase(it);
k--;
}
const auto target = activeIndices.begin()->second;
return { arr[target.first], arr[n - target.second] };
// 1 / 5 1 / 3 1 / 2
// 2 / 5 2 / 3
// 3 / 5
// 1 / 5
// 1 / 3
// 2 / 5 <
// 1 / 2
// 3 / 5
// 2 / 3
}
};