Advertisement
VladNitu

BikeRepair Gherla Vlad

Nov 22nd, 2022
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.99 KB | None | 0 0
  1. package weblab;
  2.  
  3. import java.util.*;
  4.  
  5. class Time {
  6.  
  7.   int time;
  8.   int start;  // 1 - true, 0 - false
  9.  
  10.   public Time(int time, int start) {
  11.     this.time = time;
  12.     this.start = start;
  13.   }
  14. }
  15. class Solution {
  16.  
  17.     public static int fixMyBikesPlease(int n, int[] starttimes, int[] durations) {
  18.    
  19.       List<Time> times = new ArrayList<>();
  20.       for (int i = 1; i <= n; ++i) {
  21.         times.add(new Time(starttimes[i], 1));
  22.         times.add(new Time(starttimes[i] + durations[i], 0));
  23.       }
  24.      
  25.       Collections.sort(times, Comparator.comparing((Time t) -> t.time).thenComparing((Time t) -> t.start));
  26.      
  27.       int Max = Integer.MIN_VALUE;
  28.      
  29.       int currently_in_service = 0;
  30.       for (Time t: times) {
  31.      
  32.         if (t.start == 1) {
  33.           currently_in_service ++;
  34.           Max = Math.max(Max, currently_in_service);
  35.         }  
  36.         else
  37.           currently_in_service --;
  38.        
  39.       }
  40.      
  41.       return Max;
  42.    
  43.     }
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement