From a9e7c67c72d9572aca1c5a5485c56ef11c35ea3e Mon Sep 17 00:00:00 2001 From: Dineshwar Doddapaneni <63300423+dinesh9-ai@users.noreply.github.com> Date: Thu, 6 Oct 2022 14:05:46 +0530 Subject: [PATCH] added merge overlapping subintervals --- .../Python/arrays/merge_overlapping_intervals.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 algorithms/Python/arrays/merge_overlapping_intervals.py diff --git a/algorithms/Python/arrays/merge_overlapping_intervals.py b/algorithms/Python/arrays/merge_overlapping_intervals.py new file mode 100644 index 00000000..d281d42a --- /dev/null +++ b/algorithms/Python/arrays/merge_overlapping_intervals.py @@ -0,0 +1,10 @@ +arr = [[1,2],[0,1],[3,4],[4,6],[9,10]] +arr.sort(key = lambda x : x[0]) +m = [] +for i in range(len(arr)): + if m==[] or m[-1][1] < arr[i][0]: + v=[arr[i][0],arr[i][1]] + m.append(v) + else: + m[-1][1] = max(m[-1][1],arr[i][1]) +print(*m) \ No newline at end of file