My program takes a pair of numbers and subtracts them from one another then adds all the differences from the pairs back up for a total.
My problem is that some of the numbers overlap, by numbers I am using start and end times. I need to find a way to disregard overlapping numbers so that an input of
{{10, 14}, {4, 18}, {19, 20}, {19, 20}, {13, 20}
returns a total of 16
and not 27
. Thus far I have created another array called hours to check off times already existing. Here is that portion of code:
public static int monitoring(int[][] intervals) {
int sum = 0;
int start = 0;
int end = 0;
boolean[] hours = new boolean[24];
int count = 0;
for(int i = 0; i < intervals.length; i++) {
end = intervals[i][1];
start = intervals[i][0];
for(int j = start; j < end; j++) {
hours[j] = true;
count++;
sum += end - start;
}
}
return sum;