I have code like this
public List<int[]> getMinifiedRanges(List<int[]> ranges) {
for (int[] range : ranges) {
}
}
My goal is to condense the list of ranges, represented by integer arrays which contain the ranges upper & lower bounds
ex. [100, 200] [250, 350] - this example would return the same input because nothing overlaps
[100, 200] [150, 350] [400, 500] - this would return [100, 350] [400, 500] because the second ranges lower bound is inclusive in the first range so the returned range would have its upper bound extended to 350 - notice how it returns only two arrays when the input was 3 arrays.
I am having trouble figuring out how to retrieve a previous range from a current range so that I could extend either the lower or upper bound.
int[]
variable which stores the last result or use a "normal" (non-for-each) for loop (where accessing the last element is trivial). But this problem is much easier solved by separating start and end into the same list ofint
s and iterating over that. – Bernhard Barker