-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertInterval.java
71 lines (59 loc) · 1.35 KB
/
InsertInterval.java
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
package interval;
import java.util.*;
/**
* @author Shogo Akiyama
* Solved on 12/11/2019
*
* 57. Insert Interval
* https://leetcode.com/problems/insert-interval/
* Difficulty: Hard
*
* Approach: Iteration
* Runtime: 3 ms, faster than 24.04% of Java online submissions for Insert Interval.
* Memory Usage: 41.7 MB, less than 68.75% of Java online submissions for Insert Interval.
*
* Time Complexity: O(n)
* Space Complexity: O(n)
* Where n is the number of intervals
*
* @see IntervalTest#testInsertInterval()
*/
public class InsertInterval {
public int[][] insert(int[][] intervals, int[] newInterval) {
List<int[]> list = new ArrayList<int[]>();
boolean added = false;
for (int[] i : intervals) {
if (!added && i[0] >= newInterval[0]) {
list.add(newInterval);
added = true;
}
list.add(i);
}
if (!added) {
list.add(newInterval);
}
int index = 1;
int count = 0;
while (index < list.size()) {
int[] prev = list.get(index - 1);
int[] curr = list.get(index);
if (prev[1] >= curr[0]) {
curr[0] = prev[0];
curr[1] = Math.max(curr[1], prev[1]);
prev[0] = -1;
prev[1] = -1;
count++;
}
index++;
}
int[][] result = new int[list.size() - count][2];
int j = 0;
for (int[] i : list) {
if (i[0] != -1 && i[1] != -1) {
result[j] = i;
j++;
}
}
return result;
}
}