|
10 | 10 |
|
11 | 11 |
|
12 | 12 | #### Basic Implementation
|
13 |
| -- 这里已经给了sorted intervals by start point. |
| 13 | +- 这里已经给了 **sorted** intervals by start point. |
14 | 14 | - 直接找到可以insert newInterval的位子. Insert
|
15 | 15 | - 然后loop to merge entire interval array
|
16 | 16 | - 因为给的是个list, 所以方便`intervals.remove(i)`
|
|
58 | 58 | 2. sort point in min-heap
|
59 | 59 | 3. when count increase and decreases to 0, that means we can close an interval
|
60 | 60 | */
|
61 |
| -/** |
62 |
| - * Definition for an interval. |
63 |
| - * public class Interval { |
64 |
| - * int start; |
65 |
| - * int end; |
66 |
| - * Interval() { start = 0; end = 0; } |
67 |
| - * Interval(int s, int e) { start = s; end = e; } |
68 |
| - * } |
69 |
| - */ |
70 |
| -public class Solution { |
71 |
| - |
| 61 | +/* |
| 62 | +Option1: |
| 63 | +- convert all intervals into points with open/close flag, sort, and merge. When count == 0, close a interval. |
| 64 | +- priority queue, O(n) space, O(nlogn) time |
| 65 | +*/ |
| 66 | +class Solution { |
72 | 67 | class Point {
|
73 |
| - int x; |
74 |
| - int flag; |
75 |
| - public Point(int x, int flag) { |
76 |
| - this.x = x; |
| 68 | + int val, flag; |
| 69 | + public Point(int val, int flag) { |
| 70 | + this.val = val; |
77 | 71 | this.flag = flag;
|
78 | 72 | }
|
79 | 73 | }
|
80 | 74 | public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
|
81 | 75 | List<Interval> rst = new ArrayList<>();
|
82 |
| - if (intervals == null && newInterval == null) { |
83 |
| - return rst; |
84 |
| - } else if (intervals == null) { |
85 |
| - rst.add(newInterval); |
| 76 | + if (intervals == null || intervals.size() == 0 || newInterval == null) { |
| 77 | + if (newInterval != null) rst.add(newInterval); |
86 | 78 | return rst;
|
87 |
| - } else if (newInterval == null) { |
88 |
| - return intervals; |
89 | 79 | }
|
90 |
| - |
91 |
| - PriorityQueue<Point> queue = new PriorityQueue<>(1, new Comparator<Point>(){ |
92 |
| - public int compare(Point a, Point b){ |
93 |
| - return a.x - b.x; |
94 |
| - } |
95 |
| - }); |
96 |
| - |
97 |
| - for (Interval range: intervals) { |
98 |
| - queue.add(new Point(range.start, 1)); |
99 |
| - queue.add(new Point(range.end, -1)); |
100 |
| - } |
101 |
| - |
102 |
| - queue.add(new Point(newInterval.start, 1)); |
103 |
| - queue.add(new Point(newInterval.end, -1)); |
104 |
| - |
| 80 | + |
| 81 | + // build priority queue |
| 82 | + PriorityQueue<Point> queue = buildQueue(intervals, newInterval); |
| 83 | + // iterate over queue, count and build interval |
105 | 84 | int count = 0;
|
106 |
| - int startNew = 0; |
107 |
| - int endNew = 0; |
| 85 | + Interval interval = new Interval(); |
108 | 86 | while (!queue.isEmpty()) {
|
109 | 87 | Point p = queue.poll();
|
110 |
| - if (count == 0) { |
111 |
| - startNew = p.x; |
112 |
| - } |
| 88 | + if (count == 0) {//detect start |
| 89 | + interval.start = p.val; |
| 90 | + } |
| 91 | + |
113 | 92 | count += p.flag;
|
114 |
| - |
115 |
| - while (!queue.isEmpty() && p.x == queue.peek().x) { |
| 93 | + while (!queue.isEmpty() && p.val == queue.peek().val) { |
116 | 94 | p = queue.poll();
|
117 | 95 | count += p.flag;
|
118 | 96 | }
|
119 |
| - |
| 97 | + |
120 | 98 | if (count == 0) {
|
121 |
| - endNew = p.x; |
122 |
| - Interval addInterval = new Interval(startNew, endNew); |
123 |
| - rst.add(addInterval); |
| 99 | + interval.end = p.val; |
| 100 | + rst.add(interval); |
| 101 | + interval = new Interval(); |
124 | 102 | }
|
125 |
| - |
126 |
| - }//end while |
127 |
| - |
| 103 | + } |
128 | 104 | return rst;
|
129 |
| - |
130 | 105 | }
|
| 106 | + |
| 107 | + private PriorityQueue<Point> buildQueue(List<Interval> intervals, Interval newInterval) { |
| 108 | + PriorityQueue<Point> queue = new PriorityQueue<>(Comparator.comparing(p -> p.val)); |
| 109 | + queue.offer(new Point(newInterval.start, 1)); |
| 110 | + queue.offer(new Point(newInterval.end, -1)); |
| 111 | + for (Interval interval : intervals) { |
| 112 | + queue.offer(new Point(interval.start, 1)); |
| 113 | + queue.offer(new Point(interval.end, -1)); |
| 114 | + } |
| 115 | + return queue; |
| 116 | + } |
| 117 | + |
131 | 118 | }
|
132 | 119 |
|
133 | 120 |
|
134 | 121 |
|
| 122 | + |
135 | 123 | /*
|
136 | 124 | O(n) time, O(1) space
|
137 | 125 | Thoughts:
|
|
0 commit comments