-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge_intervals_2ndtime.php
More file actions
81 lines (73 loc) · 2.01 KB
/
Merge_intervals_2ndtime.php
File metadata and controls
81 lines (73 loc) · 2.01 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
<?php
class Solution
{
function merge($intervals)
{
sort($intervals);
$ans = [];
$ans[] = $intervals[0];
$i = 1;
$n = count($intervals) - 1;
while ($i <= $n) {
$a = $ans[count($ans) - 1][0];
$b = $ans[count($ans) - 1][1];
$c = $intervals[$i][0];
$d = $intervals[$i][1];
if ($a < $c && $b < $c) {
$ans[] = $intervals[$i];
$i++;
} else if ($a <= $c && $c <= $b && $d >= $b) {
array_pop($ans);
$ans[] = [$a, $d];
$i++;
} else if ($a < $c && $b > $d) {
$i++;
}
}
return $ans;
}
}
// class Solution {
// /**
// * @param Integer[][] $intervals
// * @return Integer[][]
// */
// function merge($intervals) {
// sort($intervals);
// $start=$intervals[0][0];
// $end = $intervals[0][1];
// $resultArr = array();
// $resultArr[0] = $intervals[0];
// $i=1;
// $j=0;
// while($i<count($intervals)){
// $curr_arr = $intervals[$i];
// $curr_start = $curr_arr[0];
// $curr_end = $curr_arr[1];
// if($curr_start<=$end){
// if($curr_end>$end){
// $end = $curr_end;
// }
// if($curr_start<$start){
// $start=$curr_start;
// }
// $resultArr[$j][0]=$start;
// $resultArr[$j][1]=$end;
// }else{
// $j++;
// $resultArr[$j][0]=$curr_start;
// $resultArr[$j][1]=$curr_end;
// $start=$curr_start;
// $end = $curr_end;
// }
// $i++;
// }
// return $resultArr;
// }
// }
$obj = new Solution();
$intervals[] = [1, 4];
$intervals[] = [2, 3];
echo "<pre>";
print_r($obj->merge($intervals));
echo "</pre>";