The obstacle
Compose a function called sumIntervals
/ sum_intervals()
that accepts a selection of periods, and returns the amount of all the interval lengths. Overlapping periods ought to just be counted when.
Periods
Periods are represented by a set of integers in the kind of a selection. The very first worth of the period will constantly be less than the 2nd worth. Period example: [1, 5]
is a period from 1 to 5. The length of this period is 4.
Overlapping Periods
List including overlapping periods:
[
[1,4],.
[7, 10],.
[3, 5]
]
The amount of the lengths of these periods is 7. Because [1, 4] and [3, 5] overlap, we can deal with the period as [1, 5], which has a length of 4.
Examples:
sumIntervals( [
[1,2],.
[6, 10],.
[11, 15]
] => > 9.
sumIntervals( [
[1,4],.
[7, 10],.
[3, 5]
] => > 7.
sumIntervals( [
[1,5],.
[10, 20],.
[1, 6],.
[16, 19],.
[5, 11]
] => > 19.
sumIntervals( [
[0, 20],.
[-100000000, 10],.
[30, 40]
] => > 100000030.
Tests with big periods
Your algorithm ought to have the ability to manage big periods. All checked periods are subsets of the variety [-1000000000, 1000000000]
The service in Java
Alternative 1:
plan cw;.
import java.util.Arrays;.
import java.util.Comparator;.
public class Interval {
public fixed int sumIntervals( int[][] periods) {
if (periods == null|| intervals.length < < 1) {
return 0;.
}
Arrays.sort( periods, Comparator.comparingInt( a -> > a[0]));.
int outcome = 0;.
int currentIntervalEnd = periods[0][0];.
for (int[] period: periods) {
int intervalStart = period[0];.
int intervalEnd = period[1];.
if (intervalEnd > > currentIntervalEnd) {
outcome += intervalEnd - Math.max( intervalStart, currentIntervalEnd);.
currentIntervalEnd = intervalEnd;.
}
}
return outcome;.
}
}
Alternative 2:
plan cw;.
import java.util. *;.
public class Interval {
last fixed personal Comparator<< int[]> > CMP_RNG = Comparator.<< int[]>> comparingInt( rng -> > rng[0]);.
public fixed int sumIntervals( int[][] periods) {
if (periods== null) return 0;.
int s = 0,.
top = Integer.MIN _ WORTH;.
int[][] varies = Arrays.copyOf( periods, intervals.length);.
Arrays.sort( varieties, CMP_RNG);.
for (int[] rng: varieties) {
if (leading<< rng[0]) top = rng[0];.
if (leading<< rng[1]) {s += rng[1]- top; top = rng[1];}
}
return s;.
}
}
Alternative 3:
plan cw;.
import java.util.Arrays;.
public class Interval {
public fixed int sumIntervals( int[][] periods) {
Arrays.sort( periods, (a, b) -> > Integer.compare( b[1], a[1]));.
return Arrays.stream( periods)
. mapToInt( ints -> > {
int[] temperature = brand-new int[] {ints[0], ints[1]};.
Arrays.fill( ints, Integer.MAX _ WORTH);.
Arrays.stream( periods). forEach( arr -> > {
if (( arr[0] >>= temperature[0] && & & arr[0] = temperature[1] & & arr[1] < = temperature[0])) {temperature[1] = Math.min( temperature[1], arr [0]);
.
temperature(* )= Math.max( temperature[0], arr(
* ));.
Arrays.fill( arr, Integer.MAX _ WORTH);
.
}});. return temperature[0]- temperature[1];.}
). amount();.
}}[1] Test cases to confirm our service[1] #[1] import org.junit.Test;
. import fixed cw.Interval.sumIntervals;. import fixed org.junit.Assert.assertEquals;. public class IntervalTest {
@Test.
public space shouldHandleEmptyIntervals() {
assertEquals( 0, sumIntervals( brand-new int[0] {} ));.
assertEquals( 0, sumIntervals( brand-new int
{{4, 4}, {6, 6}, {8, 8}} ));.
}
@Test.
public space shouldAddDisjoinedIntervals() {
assertEquals( 9, sumIntervals( brand-new int
{{4, 8}, {9, 10}, {15, 21}} ));.
assertEquals( 7, sumIntervals( brand-new int[][] {{-1, 4}, {-5, -3}} ));.
assertEquals( 78, sumIntervals( brand-new int[][] {{-245, -218}, {-194, -179}, {-155, -119}} ));.
}
@Test.
public space shouldHandleLargeIntervals() {
assertEquals( 2_000_000_000, sumIntervals( brand-new int[][] {{-1 _ 000_000_000, 1_000_000_000}} ));.
assertEquals( 100_000_030, sumIntervals( brand-new int[][] {{0, 20}, {-100 _ 000_000, 10}, {30, 40}} ));.
}
@Test.
public space shouldAddAdjacentIntervals() {
assertEquals( 54, sumIntervals( brand-new int[][] {{1, 2}, {2, 6}, {6, 55}} ));.
assertEquals( 23, sumIntervals( brand-new int[][] {{-2, -1}, {-1, 0}, {0, 21}} ));.
}
@Test.
public space shouldAddOverlappingIntervals() {
assertEquals( 7, sumIntervals( brand-new int[][] {{1, 4}, {7, 10}, {3, 5}} ));.
assertEquals( 6, sumIntervals( brand-new int[][] {{5, 8}, {3, 6}, {1, 2}} ));.
assertEquals( 19, sumIntervals( brand-new int[][] {{1, 5}, {10, 20}, {1, 6}, {16, 19}, {5, 11}} ));.
}
@Test.
public space shouldHandleMixedIntervals() {
assertEquals( 13, sumIntervals( brand-new int[][] {{2, 5}, {-1, 2}, {-40, -35}, {6, 8}} ));.
assertEquals( 1234, sumIntervals( brand-new int[][] {{-7, 8}, {-2, 10}, {5, 15}, {2000, 3150}, {-5400, -5338}} ));.
assertEquals( 158, sumIntervals( brand-new int[][] {{-101, 24}, {-35, 27}, {27, 53}, {-105, 20}, {-36, 26},} ));.
}
}
[][]