Last active
March 16, 2021 21:41
-
-
Save mvallebr/11e5fcc619cd3488af9f7216615c14e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from typing import List | |
| class Solution: | |
| def insertInterval(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]: | |
| def overlap(interval1, interval2): | |
| # print(f"interval1 {interval1}, interval2 {interval2}") | |
| return interval2[1] >= interval1[0] and interval2[0] <= interval1[1] | |
| result = [] | |
| i = 0 | |
| while i < len(intervals) and not overlap(intervals[i], newInterval) and intervals[i] < newInterval: | |
| result.append(intervals[i]) | |
| i += 1 | |
| result.append(newInterval) | |
| while i < len(intervals): | |
| if not result or not overlap(result[-1], intervals[i]): | |
| result.append(intervals[i]) | |
| else: | |
| result[-1][0] = min(result[-1][0], intervals[i][0]) | |
| result[-1][1] = max(result[-1][1], intervals[i][1]) | |
| i += 1 | |
| return result | |
| if __name__ == "__main__": | |
| sol = Solution() | |
| newInterval = [int(x) for x in input().split()] | |
| intervalsRange = int(input()) | |
| intervals = [[int(x) for x in input().split()] for _ in range(intervalsRange)] | |
| result = sol.insertInterval(intervals, newInterval) | |
| for res in result: | |
| print(' '.join(str(num) for num in res)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment