Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package interviewbit;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Comparator;
- public class HotelReservation {
- public boolean hotel(ArrayList<Integer> arrive, ArrayList<Integer> depart, int K) {
- arrive.sort(Comparator.naturalOrder());
- depart.sort(Comparator.naturalOrder());
- int n = arrive.size();
- int rooms_max = 0,i=0,j=0,rooms=0;
- while(i < n && j < n) {
- if(arrive.get(i) < depart.get(j)) {
- rooms++;
- i++;
- if(rooms > rooms_max) {
- rooms_max = rooms;
- }
- }else {
- rooms--;
- j++;
- }
- }
- return rooms_max > K ? false : true;
- }
- public static void main(String[] args) {
- ArrayList<Integer> a = new ArrayList(Arrays.asList(1,2,3));
- ArrayList<Integer> b = new ArrayList(Arrays.asList(2,3,4));
- int k = 1;
- Solution s = new Solution();
- System.out.println(s.hotel(a, b, k));
- }
- }
Add Comment
Please, Sign In to add comment