Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
1.4 kB
2
Indexable
Never
package weblab;

import java.util.*;

class Solution {

public static int fixMyBikesPlease(int n, int[] starttimes, int[] durations) {
  
        PriorityQueue<Job> jobs = new PriorityQueue<>();

        for(int i = 1; i < starttimes.length; i++) {
            jobs.add(new Job(starttimes[i], starttimes[i]+durations[i]));
        }

        int count = 0;
        int nbEmployees = 0;

        int[] array = new int[n];
        for(int i = 0; i < n; i++) array[i] = 0;

        while(count != n) {

            Job currJob = jobs.remove();

            for(int i = 0; i < array.length; i++) {

                if(array[i] == 0) {

                    if(currJob.starttime < array[i-1]) {
                        nbEmployees++;
                        array[i] = currJob.duration;
                    } 
                }
            }
        }
        return nbEmployees;
}

    static class Job implements Comparable<Job> {

        int starttime;
        int duration;

        public Job(int starttime, int duration) {
            this.starttime = starttime;
            this.duration = duration;
        }

        @Override
        public int compareTo(Job o) {
            int result = o.duration- this.duration;

            if(result == 0) return this.starttime - o.starttime;

            return result;
        }
    }

}