Skip to content

Instantly share code, notes, and snippets.

@gladimdim
Last active October 12, 2021 15:28
Show Gist options
  • Save gladimdim/7d1eb94e30d41b48172355d4adabe15f to your computer and use it in GitHub Desktop.
Save gladimdim/7d1eb94e30d41b48172355d4adabe15f to your computer and use it in GitHub Desktop.
/// Task
/// Given a set of closed intervals, find the smallest set of numbers that covers all the intervals. If there are multiple smallest sets, return any of them.
Set findMinSetToCoverIntervals(List<List<int>> input) {
var start;
var end;
/// we need to find minimum value of the end intervals, it will be our start in result
/// and maximum value of the start intervals, it will be our end in result
for (List<int> interval in input) {
start ??= interval[1];
end ??= interval[0];
if (interval[0] > end) {
end = interval[0];
}
if (interval[1] < start) {
start = interval[1];
}
}
return {start, end};
}
void main() {
var result = findMinSetToCoverIntervals([
[10, 20],
[6, 9],
[0, 3],
[2, 6],
[3, 4],
]);
print(result);
result = findMinSetToCoverIntervals([
[0, 4],
[2, 6],
[0, 9]
]);
print(result);
result = findMinSetToCoverIntervals([
[-20, 10],
[50, 100],
[-100, 0],
[500, 501],
]);
print(result);
result = findMinSetToCoverIntervals([
[0, 3],
[2, 6],
[3, 4],
[6, 9],
]);
print(result);
result = findMinSetToCoverIntervals([
[0, 10],
[15, 20]
]);
print(result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment