Compare commits

..

3 commits

Author SHA1 Message Date
Bruno BELANYI 14e8b539c6 2022: d15: ex2: add solution 2022-12-15 17:15:57 +01:00
Bruno BELANYI 0f91c37d6d 2022: d15: ex2: add input 2022-12-15 17:15:57 +01:00
Bruno BELANYI 1ce063154e 2022: d15: ex1: add solution 2022-12-15 17:15:57 +01:00
2 changed files with 4 additions and 2 deletions

View file

@ -31,7 +31,8 @@ def merge_intervals(intervals: Iterable[Interval]) -> list[Interval]:
res = [intervals[0]]
for candidate in intervals[1:]:
if res[-1].end >= candidate.start:
# Range is inclusive in both end, so add 1 to end in case of near miss
if (res[-1].end + 1) >= candidate.start:
new_end = max(res[-1].end, candidate.end)
res[-1] = Interval(res[-1].start, new_end)
else:

View file

@ -31,7 +31,8 @@ def merge_intervals(intervals: Iterable[Interval]) -> list[Interval]:
res = [intervals[0]]
for candidate in intervals[1:]:
if res[-1].end >= candidate.start:
# Range is inclusive in both end, so add 1 to end in case of near miss
if (res[-1].end + 1) >= candidate.start:
new_end = max(res[-1].end, candidate.end)
res[-1] = Interval(res[-1].start, new_end)
else: