Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package weblab;
- import java.io.*;
- import java.util.*;
- class Job{
- int startTime;
- int endTime;
- public Job(int startTime, int endTime) {
- this.startTime=startTime;
- this.endTime=startTime+endTime;
- }
- }
- class Solution {
- // Implement the solve method to return the answer to the problem posed by the inputstream.
- public static String run(InputStream in) {
- return new Solution().solve(in);
- }
- public String solve(InputStream in) {
- Scanner scanner = new Scanner(in);
- int orders = scanner.nextInt();
- int noJobs = 0;
- List <Job>jobs = new ArrayList();
- while(scanner.hasNext()) {
- jobs.add(new Job(scanner.nextInt(),scanner.nextInt()));
- }
- while(!jobs.isEmpty()) {
- noJobs++;
- Job firstDone= jobs.get(0);
- //get the job that's finished the earliest
- int i =0;
- for(Job job: jobs) {
- if(job.endTime<firstDone.endTime) {
- firstDone=jobs.get(i);
- }
- i++;
- }
- //remove job from available jobs
- jobs.remove(firstDone);
- if(jobs.size()==0) {
- return Integer.toString(noJobs);
- }
- Job firstReady= jobs.get(0);
- //List <Job>toBeRemoved = new ArrayList();
- remove(jobs,firstDone,firstReady);
- }
- return Integer.toString(noJobs);
- }
- private void remove(List<Job> jobs, Job firstDone, Job firstReady) {
- int i =0;
- boolean next=false;
- //finds first available job
- for(Job job: jobs) {
- if(job.startTime>=firstDone.endTime && job.endTime<firstReady.endTime) {
- firstReady=jobs.get(i);
- next=true;
- }
- i++;
- }
- // System.out.println("1");
- jobs.remove(firstReady);
- // System.out.println("2");
- if(next&&jobs.size()!=0) {
- remove(jobs,firstReady,jobs.get(0));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement