Merge overlapping intervals into consolidated ranges, a classic algorithm for scheduling and resource allocation.
Code
Algorithmsarr = intervals.sort
merged = [arr[0]]
arr[1..].each do |interval|
if interval[0] <= merged[-1][1]
merged[-1][1] = [merged[-1][1], interval[1]].max
else
merged << interval
end
end
return mergedParameters
Array of [start, end] intervals
Server
More Ruby Snippets
Array Difference
Find elements in the first array that are not present in the second array.
Array Frequencies
Count how many times each value appears in an array and return a frequency map.
Array Head
Get the first n elements of an array.
Array Intersection
Find common elements that exist in both arrays.
Array Tail
Get the last n elements of an array.
Array Union
Combine two arrays and remove duplicates to produce a union.