| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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
- 30. Substring with Concatenation of All Words
- Decorator
- Python Implementation
- 109. Convert Sorted List to Binary Search Tree
- Regular Expression
- Python
- Generator
- 밴픽
- 715. Range Module
- Class
- DWG
- data science
- 컴퓨터의 구조
- 43. Multiply Strings
- iterator
- 프로그래머스
- shiba
- 파이썬
- t1
- kaggle
- 315. Count of Smaller Numbers After Self
- 시바견
- Protocol
- Substring with Concatenation of All Words
- attribute
- 운영체제
- Convert Sorted List to Binary Search Tree
- concurrency
- LeetCode
- Python Code
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/
Employee Free Time - LeetCode
Can you solve this real interview question? Employee Free Time - Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
"""
# 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 |