|
| 1 | +/** |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2016 Ivan Samsonov |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included |
| 14 | + * in all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | +package com.interviewbit; |
| 25 | + |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.Arrays; |
| 28 | + |
| 29 | +/** |
| 30 | + * An ${Name} class. |
| 31 | + * |
| 32 | + * <p> |
| 33 | + * Description |
| 34 | + * </p> |
| 35 | + * |
| 36 | + * @author Ivan Samsonov (hronya@gmail.com) |
| 37 | + * @since 1.0.0 |
| 38 | + * |
| 39 | + */ |
| 40 | + |
| 41 | +public class MergeIntervals { |
| 42 | + private class Interval { |
| 43 | + int start; |
| 44 | + int end; |
| 45 | + Interval() { start = 0; end = 0; } |
| 46 | + Interval(int s, int e) { start = s; end = e; } |
| 47 | + |
| 48 | + @Override |
| 49 | + public String toString() { |
| 50 | + return "[" + start + "," + end + "]"; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private int binarySearch(int[] array, int value) { |
| 55 | + if (array.length == 0) { |
| 56 | + return 0; |
| 57 | + } |
| 58 | + |
| 59 | + int from = 0; |
| 60 | + int to = array.length-1; |
| 61 | + int mid = 0; |
| 62 | + |
| 63 | + while (from <= to) { |
| 64 | + mid = (to+from) >>> 1; |
| 65 | + if (array[mid] < value) { |
| 66 | + from = mid + 1; |
| 67 | + } else if (array[mid] > value) { |
| 68 | + to = mid - 1; |
| 69 | + } else { |
| 70 | + return mid; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if (array[mid] > value) { |
| 75 | + return mid-1; |
| 76 | + } else { |
| 77 | + return mid; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private boolean overlaps(Interval i1, Interval i2) { |
| 82 | + return !((i1.start > i2.end) || (i1.end < i2.start)); |
| 83 | + } |
| 84 | + |
| 85 | + private Interval merge(Interval i1, Interval i2) { |
| 86 | + return new Interval(Math.min(i1.start, i2.start), Math.max(i1.end, i2.end)); |
| 87 | + } |
| 88 | + |
| 89 | + public ArrayList<Interval> insert(ArrayList<Interval> intervals, Interval newInterval) { |
| 90 | + ArrayList<Interval> result = new ArrayList<>(); |
| 91 | + int size = intervals.size(); |
| 92 | + int[] starts = new int[size]; |
| 93 | + for (int i = 0; i < size; i++) { |
| 94 | + starts[i] = intervals.get(i).start; |
| 95 | + } |
| 96 | + |
| 97 | + int start_index = binarySearch(starts, newInterval.start); |
| 98 | + |
| 99 | + if (start_index >= size) { |
| 100 | + start_index = size - 1; |
| 101 | + } |
| 102 | + |
| 103 | + if (start_index < 0) { |
| 104 | + start_index = 0; |
| 105 | + } |
| 106 | + |
| 107 | + for (int i = 0; i < start_index; i++) { |
| 108 | + result.add(intervals.get(i)); |
| 109 | + } |
| 110 | + |
| 111 | + Interval currentInterval = new Interval(newInterval.start, newInterval.end); |
| 112 | + int i; |
| 113 | + for (i = start_index; i < size; ++i) { |
| 114 | + if (overlaps(currentInterval, intervals.get(i))) { |
| 115 | + currentInterval = merge(currentInterval, intervals.get(i)); |
| 116 | + } else if (intervals.get(i).start < currentInterval.start) { |
| 117 | + result.add(intervals.get(i)); |
| 118 | + } else { |
| 119 | + break; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + if (i < size && intervals.get(i).start < currentInterval.start) { |
| 124 | + result.add(intervals.get(i)); |
| 125 | + i++; |
| 126 | + } |
| 127 | + |
| 128 | + result.add(currentInterval); |
| 129 | + |
| 130 | + for (; i < size; ++i) { |
| 131 | + result.add(intervals.get(i)); |
| 132 | + } |
| 133 | + |
| 134 | + return result; |
| 135 | + } |
| 136 | +} |
0 commit comments