|
| 1 | +/** |
| 2 | + * Time Complexity: O(m + n) - Two pointers |
| 3 | + * Space Complexity: O(1) - In-place modification |
| 4 | + */ |
| 5 | +class Solution { |
| 6 | + public int[][] intervalIntersection(int[][] firstList, int[][] secondList) { |
| 7 | + List<int[]> result = new ArrayList<>(); |
| 8 | + |
| 9 | + int i = 0, j = 0; |
| 10 | + |
| 11 | + while (i < firstList.length && j < secondList.length) { |
| 12 | + // Find intersection of current intervals |
| 13 | + int start = Math.max(firstList[i][0], secondList[j][0]); |
| 14 | + int end = Math.min(firstList[i][1], secondList[j][1]); |
| 15 | + |
| 16 | + // If intersection exists, add to result |
| 17 | + if (start <= end) { |
| 18 | + result.add(new int[]{start, end}); |
| 19 | + } |
| 20 | + |
| 21 | + // Move pointer of interval that ends first |
| 22 | + if (firstList[i][1] < secondList[j][1]) { |
| 23 | + i++; |
| 24 | + } else { |
| 25 | + j++; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + return result.toArray(new int[result.size()][]); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +// Alternative approach using brute force |
| 34 | +class SolutionBruteForce { |
| 35 | + public int[][] intervalIntersection(int[][] firstList, int[][] secondList) { |
| 36 | + List<int[]> result = new ArrayList<>(); |
| 37 | + |
| 38 | + for (int[] first : firstList) { |
| 39 | + for (int[] second : secondList) { |
| 40 | + int start = Math.max(first[0], second[0]); |
| 41 | + int end = Math.min(first[1], second[1]); |
| 42 | + |
| 43 | + if (start <= end) { |
| 44 | + result.add(new int[]{start, end}); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return result.toArray(new int[result.size()][]); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// Alternative approach using iterative |
| 54 | +class SolutionIterative { |
| 55 | + public int[][] intervalIntersection(int[][] firstList, int[][] secondList) { |
| 56 | + List<int[]> result = new ArrayList<>(); |
| 57 | + |
| 58 | + int i = 0, j = 0; |
| 59 | + |
| 60 | + while (i < firstList.length && j < secondList.length) { |
| 61 | + int start = Math.max(firstList[i][0], secondList[j][0]); |
| 62 | + int end = Math.min(firstList[i][1], secondList[j][1]); |
| 63 | + |
| 64 | + if (start <= end) { |
| 65 | + result.add(new int[]{start, end}); |
| 66 | + } |
| 67 | + |
| 68 | + if (firstList[i][1] < secondList[j][1]) { |
| 69 | + i++; |
| 70 | + } else { |
| 71 | + j++; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return result.toArray(new int[result.size()][]); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// Alternative approach using while loop |
| 80 | +class SolutionWhileLoop { |
| 81 | + public int[][] intervalIntersection(int[][] firstList, int[][] secondList) { |
| 82 | + List<int[]> result = new ArrayList<>(); |
| 83 | + |
| 84 | + int i = 0, j = 0; |
| 85 | + |
| 86 | + while (i < firstList.length && j < secondList.length) { |
| 87 | + int start = Math.max(firstList[i][0], secondList[j][0]); |
| 88 | + int end = Math.min(firstList[i][1], secondList[j][1]); |
| 89 | + |
| 90 | + if (start <= end) { |
| 91 | + result.add(new int[]{start, end}); |
| 92 | + } |
| 93 | + |
| 94 | + if (firstList[i][1] < secondList[j][1]) { |
| 95 | + i++; |
| 96 | + } else { |
| 97 | + j++; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return result.toArray(new int[result.size()][]); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +// Alternative approach using enhanced for loop |
| 106 | +class SolutionEnhancedForLoop { |
| 107 | + public int[][] intervalIntersection(int[][] firstList, int[][] secondList) { |
| 108 | + List<int[]> result = new ArrayList<>(); |
| 109 | + |
| 110 | + int i = 0, j = 0; |
| 111 | + |
| 112 | + while (i < firstList.length && j < secondList.length) { |
| 113 | + int start = Math.max(firstList[i][0], secondList[j][0]); |
| 114 | + int end = Math.min(firstList[i][1], secondList[j][1]); |
| 115 | + |
| 116 | + if (start <= end) { |
| 117 | + result.add(new int[]{start, end}); |
| 118 | + } |
| 119 | + |
| 120 | + if (firstList[i][1] < secondList[j][1]) { |
| 121 | + i++; |
| 122 | + } else { |
| 123 | + j++; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return result.toArray(new int[result.size()][]); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +// Alternative approach using recursive |
| 132 | +class SolutionRecursive { |
| 133 | + public int[][] intervalIntersection(int[][] firstList, int[][] secondList) { |
| 134 | + List<int[]> result = new ArrayList<>(); |
| 135 | + |
| 136 | + intervalIntersectionHelper(firstList, secondList, 0, 0, result); |
| 137 | + |
| 138 | + return result.toArray(new int[result.size()][]); |
| 139 | + } |
| 140 | + |
| 141 | + private void intervalIntersectionHelper(int[][] firstList, int[][] secondList, |
| 142 | + int i, int j, List<int[]> result) { |
| 143 | + if (i >= firstList.length || j >= secondList.length) { |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + int start = Math.max(firstList[i][0], secondList[j][0]); |
| 148 | + int end = Math.min(firstList[i][1], secondList[j][1]); |
| 149 | + |
| 150 | + if (start <= end) { |
| 151 | + result.add(new int[]{start, end}); |
| 152 | + } |
| 153 | + |
| 154 | + if (firstList[i][1] < secondList[j][1]) { |
| 155 | + intervalIntersectionHelper(firstList, secondList, i + 1, j, result); |
| 156 | + } else { |
| 157 | + intervalIntersectionHelper(firstList, secondList, i, j + 1, result); |
| 158 | + } |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +// More concise version |
| 163 | +class SolutionConcise { |
| 164 | + public int[][] intervalIntersection(int[][] firstList, int[][] secondList) { |
| 165 | + List<int[]> result = new ArrayList<>(); |
| 166 | + int i = 0, j = 0; |
| 167 | + |
| 168 | + while (i < firstList.length && j < secondList.length) { |
| 169 | + int start = Math.max(firstList[i][0], secondList[j][0]); |
| 170 | + int end = Math.min(firstList[i][1], secondList[j][1]); |
| 171 | + |
| 172 | + if (start <= end) result.add(new int[]{start, end}); |
| 173 | + |
| 174 | + if (firstList[i][1] < secondList[j][1]) i++; |
| 175 | + else j++; |
| 176 | + } |
| 177 | + |
| 178 | + return result.toArray(new int[result.size()][]); |
| 179 | + } |
| 180 | +} |
0 commit comments