Similar to Job Scheduling O(nlogn) Solution Java


#1

public class Solution {
class Interval{
int start,end;
Interval(int s,int e){start=s; end=e;}
void print(){
System.out.print(start+" “+end+” ");
}
}
class Intervalsort implements Comparator{
@Override
public int compare(Interval i1,Interval i2){
int i = Integer.compare(i1.end,i2.end);
if(i==0)
i=Integer.compare(i1.start,i2.start);
return i;
}
}
public int solve(ArrayList<ArrayList> A) {
int n = A.size();
ArrayList b = new ArrayList<>();
ArrayList a = new ArrayList<>();
for(int i=0;i<n;i++){
a.add(new Interval(A.get(i).get(0),A.get(i).get(1)));
}
a.sort(new Intervalsort());
int endtime = a.get(0).end;
int count=1;
for(int i=1;i<n;i++){
//System.out.print(endtime);
if(endtime<a.get(i).start){
count++;
endtime =a.get(i).end;
}
}
return count;
}
}