daily pastebin goal
65%
SHARE
TWEET

Untitled

a guest Jun 19th, 2018 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package interviewbit;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Comparator;
  6.  
  7. public class HotelReservation {
  8.     public boolean hotel(ArrayList<Integer> arrive, ArrayList<Integer> depart, int K) {
  9.        
  10.         arrive.sort(Comparator.naturalOrder());
  11.         depart.sort(Comparator.naturalOrder());
  12.         int n = arrive.size();
  13.       int rooms_max = 0,i=0,j=0,rooms=0;
  14.       while(i < n && j < n) {
  15.             if(arrive.get(i) < depart.get(j)) {
  16.                 rooms++;
  17.                 i++;
  18.                 if(rooms > rooms_max) {
  19.                     rooms_max = rooms;
  20.                 }
  21.             }else {
  22.                 rooms--;
  23.                 j++;
  24.             }
  25.       }
  26.       return rooms_max > K ? false : true;
  27.     }
  28.    
  29.    
  30.    
  31.     public static void main(String[] args) {
  32.         ArrayList<Integer> a = new ArrayList(Arrays.asList(1,2,3));
  33.         ArrayList<Integer> b = new ArrayList(Arrays.asList(2,3,4));
  34.         int k = 1;
  35.        
  36.         Solution s = new Solution();
  37.         System.out.println(s.hotel(a, b, k));
  38.     }
  39. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top