일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- concurrency
- Class
- Regular Expression
- 파이썬
- Python
- 43. Multiply Strings
- Substring with Concatenation of All Words
- data science
- 컴퓨터의 구조
- Decorator
- Protocol
- 315. Count of Smaller Numbers After Self
- t1
- 30. Substring with Concatenation of All Words
- Python Implementation
- 프로그래머스
- Python Code
- 715. Range Module
- attribute
- Generator
- LeetCode
- 109. Convert Sorted List to Binary Search Tree
- kaggle
- 시바견
- Convert Sorted List to Binary Search Tree
- iterator
- 운영체제
- DWG
- 밴픽
- shiba
Archives
- Today
- Total
Scribbling
Overlapping Intervals/Ranges 본문
Q: Given arbitrary ranges, merge all the ranges that overlap with each other. Return the resultant ranges.
<Solution>
1. Sort all the ranges by their start and end.
2. Now that we know that range's start increases we have to take care of the ends
--> We have to take care of the two cases below.
3. Code
ranges = []
ranges.append(schedules[0])
for s, e in schedules:
if s <= ranges[-1][1]:
ranges[-1][1] = max(ranges[-1][1], e)
else:
ranges.append([s, e])
LeetCode: 759. Employee Free Time
https://leetcode.com/problems/employee-free-time/description/
"""
# Definition for an Interval.
class Interval:
def __init__(self, start: int = None, end: int = None):
self.start = start
self.end = end
"""
class Solution:
def employeeFreeTime(self, schedule: '[[Interval]]') -> '[Interval]':
schedules = []
for i in range(len(schedule)):
for j in range(len(schedule[i])):
schedules.append([schedule[i][j].start, schedule[i][j].end])
schedules.sort()
ranges = []
ranges.append(schedules[0])
for s, e in schedules:
if s <= ranges[-1][1]:
ranges[-1][1] = max(ranges[-1][1], e)
else:
ranges.append([s, e])
ret = []
for i in range(1, len(ranges)):
ret.append(Interval(ranges[i-1][1], ranges[i][0]))
return ret
'Computer Science > Coding Test' 카테고리의 다른 글
[Programmers] 택배 배달과 수거하기 (0) | 2024.07.20 |
---|---|
LeetCode: 1101. The Earliest Moment When Everyone Become Friends (0) | 2023.09.14 |
[Dynamic Programming] Practice Question (0) | 2023.04.14 |
LeetCode: 2115. Find All Possible Recipes from Given Supplies (0) | 2022.05.03 |
LeetCode: 1032. Stream of Characters (0) | 2022.03.25 |