-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmallestSetOfRanges.cpp
83 lines (73 loc) · 1.97 KB
/
smallestSetOfRanges.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
http://www.careercup.com/question?id=6262661043978240
*/
#include <vector>
#include <iostream>
#include <utility>
#include <algorithm>
using namespace std;
struct Compare {
bool operator() (const pair<int, int>& a, const pair<int, int>& b) {
if (a.first == b.first) return a.second < b.second;
return a.first < b.first;
}
};
vector<pair<int, int>> smallestSetOfRanges(vector<pair<int, int>>& ranges, pair<int, int> R) {
vector<pair<int, int>> result;
int target, i, maxTarget, maxIndex;
bool match;
if (R.first == R.second) {
for (i = 0; i < ranges.size(); i++) {
if (ranges[i].first <= R.first && ranges[i].second >= R.second) {
result.push_back(ranges[i]);
break;
}
}
return result;
}
sort(ranges.begin(), ranges.end(), Compare());
target = R.first;
maxIndex = 0;
while (target < R.second) {
match = false;
maxTarget = target;
//loop to find Max traget
for (i = maxIndex; i < ranges.size(); i++) {
cout<<target<<" "<<maxTarget<<endl;
/* ranges.first |target maxTarget| ranges.second */
if (ranges[i].first <= target && ranges[i].second >= maxTarget) {
match = true;
maxTarget = ranges[i].second;
maxIndex = i;
} else if (ranges[i].first > target) {
break;
}
}
if (match == false) {
result.clear();
break;
}
result.push_back(ranges[maxIndex]);
target = maxTarget;
maxIndex++;
}
return result;
}
int main() {
vector<pair<int,int>> s;
s.push_back(make_pair(1, 2));
s.push_back(make_pair(2, 3));
s.push_back(make_pair(3, 4));
s.push_back(make_pair(4, 5));
s.push_back(make_pair(5, 6));
s.push_back(make_pair(6, 7));
s.push_back(make_pair(7, 8));
s.push_back(make_pair(8, 9));
s.push_back(make_pair(9, 10));
s.push_back(make_pair(10, 11));
pair<int,int> r = make_pair(1, 11);
vector<pair<int,int> > x = smallestSetOfRanges(s,r);
for (int i = 0; i<x.size(); ++i){
cout<<"("<<x[i].first<<","<<x[i].second<<")"<<endl;
}
}