-
Notifications
You must be signed in to change notification settings - Fork 4
/
k_list_range.cpp
67 lines (61 loc) · 1.65 KB
/
k_list_range.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
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
/**
* Author: Skylar Payne
* December 25, 2014
* You have k lists of sorted integers. Find the smallest range including at least one number
* from each of the k lists.
**/
#include <iostream>
#include <vector>
int get_min(std::vector<int> const& a) {
int m = a[0];
int m_ind = 0;
for(int i = 1; i < a.size(); ++i) {
if(a[i] < m) {
m = a[i];
m_ind = i;
}
}
return m_ind;
}
int get_max(std::vector<int> const& a) {
int m = a[0];
int m_ind = 0;
for(int i = 1; i < a.size(); ++i) {
if(a[i] > m) {
m = a[i];
m_ind = i;
}
}
return m_ind;
}
std::pair<int, int> find_range(std::vector<std::vector<int> > const& lists) {
std::pair<int, int> range;
std::pair<int, int> smallest_range;
std::vector<int> indices(lists.size());
int min_k = 0;
int max_k = 0;
std::vector<int> current(lists.size());
for(int i = 0; i < lists.size(); ++i) {
current[i] = lists[i][0];
}
min_k = get_min(current);
max_k = get_max(current);
range.first = lists[min_k][indices[min_k]];
range.second = lists[max_k][indices[max_k]];
smallest_range = range;
while(++indices[min_k] < lists[min_k].size()) {
current[min_k] = lists[min_k][indices[min_k]];
min_k = get_min(current);
max_k = get_max(current);
range.first = lists[min_k][indices[min_k]];
range.second = lists[max_k][indices[max_k]];
smallest_range = (range.second - range.first < smallest_range.second - smallest_range.first ? range : smallest_range);
}
return smallest_range;
}
int main() {
std::vector<std::vector<int> > lists{{4,10,15,24,26},{0,9,12,20},{5,18,22,30}};
std::pair<int,int> r = find_range(lists);
std::cout << "[" << r.first << "," << r.second << "]" << std::endl;
return 0;
}