JAVA AC soln: using Min Heap


#1

import java.util.*;
class pair implements Comparable{
char ch;
int x;
int col;
pair(char ch,int x,int col){
this.ch=ch;
this.x=x;
this.col=col;
}
public int compareTo(pair p){
return this.x-p.x;
}
}
public class Solution {
public int solve(ArrayList a, ArrayList b, ArrayList c) {
PriorityQueue q = new PriorityQueue<>();
int min_range=Integer.MAX_VALUE;
q.add(new pair(‘a’,a.get(0),0));
q.add(new pair(‘b’,b.get(0),0));
q.add(new pair(‘c’,c.get(0),0));
int max=Math.max(a.get(0),Math.max(b.get(0),c.get(0)));
while(true){
pair p=q.remove();
int col=p.col;
char ch=p.ch;
int x=p.x;
int range=max-x;
if(range<min_range){
min_range=range;
}
if(ch==‘a’){
if(col==a.size()-1){
break;
}
else{
max=Math.max(max,a.get(col+1));
q.add(new pair(‘a’,a.get(col+1),col+1));
}
}else if(ch==‘b’){
if(col==b.size()-1){
break;
}
else{
max=Math.max(max,b.get(col+1));
q.add(new pair(‘b’,b.get(col+1),col+1));
}
}else{
if(col==c.size()-1){
break;
}
else{
max=Math.max(max,c.get(col+1));
q.add(new pair(‘c’,c.get(col+1),col+1));
}
}
}
return min_range;
}
}