-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathSolution.php
45 lines (39 loc) · 999 Bytes
/
Solution.php
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
<?php
/**
* Definition for an interval.
* class Interval {
* public $start = 0;
* public $end = 0;
* function __construct(int $start = 0, int $end = 0) {
* $this->start = $start;
* $this->end = $end;
* }
* }
*/
class Solution {
/**
* @param Interval[] $intervals
* @param Interval $newInterval
* @return Interval[]
*/
function insert($intervals, $newInterval) {
$res = [];
$s = $newInterval->start;
$e = $newInterval->end;
foreach ($intervals as $i) {
if ($i->start > $e) {
array_push($res, new Interval($s, $e));
$s = $i->start;
$e = $i->end;
}
if ($s <= $i->end) {
$s = min($s, $i->start);
$e = max($e, $i->end);
} else {
array_push($res, $i);
}
}
array_push($res, new Interval($s, $e));
return $res;
}
}