-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge_Intervals.php
More file actions
33 lines (32 loc) · 891 Bytes
/
Merge_Intervals.php
File metadata and controls
33 lines (32 loc) · 891 Bytes
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
<?php
class Solution
{
/**
* @param Integer[][] $intervals
* @return Integer[][]
*/
function merge($intervals)
{
sort($intervals);
$output = array();
foreach ($intervals as $key => $value) {
if (count($output) == 0) {
$output[] = $value;
} else {
$a = $output[count($output) - 1];
if ($value[0] <= $a[1] && $value[1] >= $a[1]) {
$x = array();
$x[] = $a[0];
$x[] = $value[1];
$output[count($output) - 1] = $x;
} else if ($value[0] > $a[1] && $value[1] > $a[0]) {
$output[] = $value;
}
}
}
return $output;
}
}
$intervals = array(array(1, 4), array(2, 3));
$obj = new Solution();
$obj->merge($intervals);