Advertisement
Guest User

Leetcode

a guest
Jan 20th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.82 KB | None | 0 0
  1. class Solution {
  2.     public int[][] intervalIntersection(int[][] A, int[][] B) {
  3.         ArrayList<int[]> res = new ArrayList<int[]>();
  4.         int i=0,j=0,k = 0;
  5.         while(i<A.length && j<B.length){
  6.             int x1 = A[i][0];
  7.             int y1 = A[i][1];
  8.             int x2 = B[j][0];
  9.             int y2 = B[j][1];
  10.            
  11.             int x = Math.max(x1,x2);
  12.             int y = Math.min(y1,y2);
  13.             if(x<=y){
  14.                 // op[k] = new int[] {x,y};
  15.                 // k++;
  16.                 res.add(new int[] {x,y});
  17.             }
  18.            
  19.             if(A[i][1]<B[j][1]){
  20.                 i++;
  21.             }else{
  22.                 j++;
  23.             }
  24.            
  25.         }
  26.        
  27.         int[][] op = new int[res.size()][2];
  28.         op = res.toArray(op);
  29.         return op;
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement