File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change 1+ public class Solution {
2+ public int [][] insert (int [][] intervals , int [] newInterval ) {
3+ List <int []> list = new ArrayList <>();
4+
5+ int order = 0 ;
6+ while (order < intervals .length && intervals [order ][0 ] < newInterval [0 ]) {
7+ list .add (intervals [order ]);
8+ order ++;
9+ }
10+ list .add (newInterval );
11+ while (order < intervals .length ) {
12+ list .add (intervals [order ]);
13+ order ++;
14+ }
15+
16+ List <int []> output = new ArrayList <>();
17+ output .add (list .get (0 ));
18+
19+ for (int i = 1 ; i < list .size (); i ++) {
20+ int [] last = output .get (output .size () - 1 );
21+ int [] current = list .get (i );
22+ if (last [1 ] < current [0 ]) {
23+ output .add (current );
24+ } else {
25+ last [1 ] = Math .max (last [1 ], current [1 ]);
26+ }
27+ }
28+
29+ return output .toArray (new int [output .size ()][]);
30+ }
31+ }
You can’t perform that action at this time.
0 commit comments