File tree 1 file changed +16
-15
lines changed
scripts/algorithms/S/Set Intersection Size At Least Two
1 file changed +16
-15
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 56 ms (Top 88.46%) | Memory: 17.8 MB (Top 31.41%)
1
2
class Solution {
2
3
public:
3
-
4
+
4
5
// sort wrt. end value
5
-
6
+
6
7
static bool compare (vector<int >& a, vector<int >& b)
7
8
{
8
9
if (a[1 ] == b[1 ])
9
10
return a[0 ] < b[0 ];
10
11
else
11
12
return a[1 ] < b[1 ];
12
13
}
13
-
14
+
14
15
int intersectionSizeTwo (vector<vector<int >>& intervals) {
15
-
16
+
16
17
int n = intervals.size ();
17
-
18
+
18
19
// sort the array
19
-
20
+
20
21
sort (intervals.begin (), intervals.end (), compare);
21
-
22
+
22
23
vector<int > res;
23
-
24
+
24
25
res.push_back (intervals[0 ][1 ] - 1 );
25
-
26
+
26
27
res.push_back (intervals[0 ][1 ]);
27
-
28
+
28
29
for (int i = 1 ; i < n; i++)
29
30
{
30
31
int start = intervals[i][0 ];
31
-
32
+
32
33
int end = intervals[i][1 ];
33
-
34
+
34
35
if (start > res.back ())
35
36
{
36
37
res.push_back (end - 1 );
37
-
38
+
38
39
res.push_back (end);
39
40
}
40
41
else if (start == res.back ())
@@ -46,7 +47,7 @@ class Solution {
46
47
res.push_back (end);
47
48
}
48
49
}
49
-
50
+
50
51
return res.size ();
51
52
}
52
- };
53
+ };
You can’t perform that action at this time.
0 commit comments