Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created March 7, 2018 07:00
Show Gist options
  • Save jianminchen/4125678fd6366dc4eae0e3a339058255 to your computer and use it in GitHub Desktop.
Save jianminchen/4125678fd6366dc4eae0e3a339058255 to your computer and use it in GitHub Desktop.
Interval algorithm - code review - review the peer's code and gave out the hint to analyze the advancement of interval.
import java.io.*;
import java.util.*;
class Solution {
static int[] meetingPlanner(int[][] slotsA, int[][] slotsB, int dur) {
// your code goes here
int i=0;
int j=0;
while(i<slotsA.length && j<slotsB.length){
int[] a = slotsA[i];
int[] b = slotsB[j];
if(isThereAnyOverlap(a,b) && isFreeDurationExists(a,b,dur)){
// check is duration can exist in both
int start = Math.max(a[0],b[0]);
int end = start+dur;
return new int[] {start,end};
}
if(a[1]<b[1]){
i++;
}
else{
j++;
}
}
return new int[] {};
}
static boolean isFreeDurationExists(int[]a,int[]b, int dur){
int maxStart = Math.max(a[0],b[0]);
int minEnd = Math.min(a[1],b[1]);
int bandOfFree = minEnd-maxStart;
return bandOfFree>=dur;
}
static boolean isThereAnyOverlap(int[]a, int[]b){
// check of no overlap case
// either startb>enda || starta>endb . 10,50 0,9
/*
if(b[0]>a[1]||a[0]>b[1]){
return false;
}
return true;
*/
return !(b[0]>a[1]||a[0]>b[1]);
}
public static void main(String[] args) {
}
//[[7,12]],
//[[2,11]],
//5
// slotsA = {60-120}
// slotsB = {60-70}
// dur 8
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment