class Solution {
public int[][] intervalIntersection(int[][] A, int[][] B) {
List<int[]> ans = new ArrayList<>();
int i = 0;
int j = 0;
while(i < A.length && j < B.length){
int l = Math.max(A[i][0],B[j][0]);
int r = Math.min(A[i][1],B[j][1]);
if( l <= r){
ans.add(new int[]{l,r});
}
if(A[i][1]<B[j][1]){
i++;
}
else{
j++;
}
}
return ans.toArray(new int[ans.size()][]);
}
}