Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class MaximumGuestAtPartyOofN {
  4.  
  5. public static void findMaxGuests(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.  
  10. int size = arrivals.length;
  11. // Find the time when the last guest left the part
  12. int last_time = Arrays.stream(departures).max().getAsInt();
  13.  
  14. int []count = new int[last_time+1];
  15.  
  16. //fill the count array
  17. for(int i = 0; i < size; i++) {
  18. //When guest comes in, do +1 at the arrival time index
  19. ++count[arrivals [i]];
  20. // guest leaves the party, do -1 at the departure time index
  21. --count[departures[i]];
  22. }
  23.  
  24. int currentGuest=0;
  25. int time=0;
  26. int maxGuest=0;
  27.  
  28. for(int i = 0; i < last_time; i++) {
  29. currentGuest += count[i];
  30. if(maxGuest < currentGuest) {
  31. maxGuest = currentGuest;
  32. time = i;
  33. }
  34. }
  35. System.out.println("Maximum Guests at party are: "+ maxGuest+ " 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. findMaxGuests(arrivals,departures);
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement