-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path56.cpp
More file actions
executable file
·37 lines (37 loc) · 1.09 KB
/
56.cpp
File metadata and controls
executable file
·37 lines (37 loc) · 1.09 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
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int> > merge(vector<vector<int> >& intervals) {
if (intervals.size() == 0)
return {};
vector<vector<int> > answer;
int max = 0;
for (int i = 0; i < intervals.size();i++)
if (intervals[i][1] > max) max = intervals[i][1];
int buckets[max+5];
for (int i = 0; i < max+5;i++)
buckets[i] = 0;
for (int i = 0; i < intervals.size();i++)
for (int j = intervals[i][0]; j <= intervals[i][1]; j++)
buckets[j] = 1;
int j = 0;
while(buckets[j] == 0)
j++;
int start;
start = -1;
for (int i = j; i < max+5;i++){
if (buckets[i] == 1 && start == -1)
start = i;
if (buckets[i] == 0 && start != -1){
vector<int> tmp;
tmp.push_back(start);
tmp.push_back(i-1);
answer.push_back(tmp);
start = -1;
}
}
return answer;
}
};