-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgigwalk.cpp
More file actions
117 lines (67 loc) · 2.08 KB
/
gigwalk.cpp
File metadata and controls
117 lines (67 loc) · 2.08 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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//method
//it takes integer input n
//return true if n has integer sqrt, otherwise false
//n 16, 4 true
//7, 2.xx false
//not going to use sqrt lib
1 true
2 false
bool isPerfectSquare(int n) {
int i = 1;
while (true) {
int mult = i*i;
if (mult > n) return false;
if (mult == n) return true;
++i;
}
return false;
}
bool isPerfectSquare2(int n){
int low = 0;
int high = n;
while (high >= low) {
int mid = low + (high-low)/2;
int mult = mid*mid;
if (mult == n) return true;
if (mult < n) low = mid - 1;
else high = mid+1;
}
return false;
}
//2B, 1,2,3,4
//n 100,
// a set of intervals
//(0,2) (-1, 3) (2,3) (15,21) (start, end)
//method that will merge these intervals if they are partially overlapped
// (-1,3) (15,21)
(-1 3) a (0 2) b (2 3) (15 21)
typedef pair<int, int> Interval;
class Compare() {
bool operator < (const Interval & a, const Interval & b) {
return (a.first > b.first);
}
}
stack<Interval> mergeIntervals(vector< pair<int, int> > & v) {
sort(v.begin(), v.end(), Compare());
stack<Interval> s;
for (int i = 0; i < v.size() ++i) {
Interval current = v[i];
if ( i == 0 ) {
s.push(current);
continue;
}
Interval prev = s.top();
if (current.first > prev.second) {
//No overlap
s.push(current);
} else {
//Merge current and prev, then push the result
Interval mergedInterval = make_pair( std::min(prev.first, current.first),
std::max(prev.second, current.second));
s.pop(); //Pop previous
s.push(mergedInterval);
}
}
return s;
}
}