Created
March 7, 2018 07:00
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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