Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class MaximumGuestAtParty {
  4.  
  5. public static void findMaximumGuest(int [] arrivals, int [] departures){
  6.  
  7. System.out.println("Guest arrival times: " + Arrays.toString(arrivals));
  8. System.out.println("Guest departure times: " + Arrays.toString(departures));
  9. int maxGuests = 0;
  10. int currentGuests = 0;
  11. int time = -1;
  12. Arrays.sort(arrivals);
  13. Arrays.sort(departures);
  14.  
  15. int i = 0;
  16. int j = 0;
  17. int size=arrivals.length;
  18. while(i< size && j < size){
  19. //pick minimum of arrival[i] and departure[j] to decide whether what is happening next
  20. //arrival[i]<departure[j] then guest is arriving to party
  21. //arrival[i]>departure[j] then guest is leaving the party.
  22. if(arrivals[i] <= departures[j]){
  23. currentGuests++;
  24. if(maxGuests<currentGuests){
  25. maxGuests = currentGuests;
  26. time = arrivals[i];
  27. }
  28. i++;
  29. }
  30. else{
  31. currentGuests--;
  32. j++;
  33. }
  34. }
  35. System.out.println("Maximum guest at party: " + maxGuests + " at time: " + time);
  36. }
  37.  
  38. public static void main(String[] args) {
  39. int arrivals[] = {0, 3, 1, 7, 4};
  40. int departures[] = {2, 7, 5, 8, 6};
  41. findMaximumGuest(arrivals, departures);
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement