Interval List Intersections LeetCode solution in Java
1class Solution {2public int[][] intervalIntersection(int[][] A, int[][] B) {3List<int[]> ans = new ArrayList<>();4int i = 0, j = 0;5while (i < A.length && j < B.length) {6int start = Math.max(A[i][0], B[j][0]);7int end = Math.min(A[i][1], B[j][1]);8if (start <= end) ans.add(new int[] {start, end});9if (A[i][1] > B[j][1]) j++;10else i++;11}1213int[][] res = new int[ans.size()][2];14i = 0;15for (int[] pair : ans) {16res[i++] = pair;17}1819return res;20}21}