Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3e9ff53

Browse files
committed
Add solution 57.
1 parent 897141e commit 3e9ff53

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Definition for an interval.
3+
* class Interval {
4+
* public $start = 0;
5+
* public $end = 0;
6+
* function __construct(int $start = 0, int $end = 0) {
7+
* $this->start = $start;
8+
* $this->end = $end;
9+
* }
10+
* }
11+
*/
12+
class Solution {
13+
14+
/**
15+
* @param Interval[] $intervals
16+
* @param Interval $newInterval
17+
* @return Interval[]
18+
*/
19+
function insert($intervals, $newInterval) {
20+
$res = [];
21+
22+
$s = $newInterval->start;
23+
$e = $newInterval->end;
24+
25+
foreach($intervals as $i) {
26+
if($i->start > $e) {
27+
array_push($res, new Interval($s,$e));
28+
$s = $i->start;
29+
$e = $i->end;
30+
}
31+
32+
if($s <= $i->end) {
33+
$s = min($s, $i->start);
34+
$e = max($e, $i->end);
35+
}
36+
else {
37+
array_push($res, $i);
38+
}
39+
}
40+
41+
array_push($res, new Interval($s,$e));
42+
return $res;
43+
}
44+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /