mirror of
https://github.com/prdlk/leetcode.git
synced 2026-11-03 05:27:36 +00:00
85 lines
3.5 KiB
Plaintext
85 lines
3.5 KiB
Plaintext
---
|
|
title: '56. Merge Intervals'
|
|
description: Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input
|
|
icon: dot
|
|
topics:
|
|
- { name: "Array", slug: "array" }
|
|
- { name: "Sorting", slug: "sorting" }
|
|
- { name: "Quicksort", slug: "quicksort" }
|
|
issue: "https://github.com/prdlk/leetcode/issues/182"
|
|
companies:
|
|
- { name: "Palo Alto Networks", period: "Thirty Days", frequency: 100 }
|
|
- { name: "Bloomberg", period: "Thirty Days", frequency: 83.1 }
|
|
- { name: "Amazon", period: "Thirty Days", frequency: 75.8 }
|
|
- { name: "Infosys", period: "Thirty Days", frequency: 68.6 }
|
|
- { name: "Microsoft", period: "Thirty Days", frequency: 67.9 }
|
|
- { name: "Meta", period: "Thirty Days", frequency: 53.8 }
|
|
- { name: "Google", period: "Thirty Days", frequency: 36.7 }
|
|
- { name: "Apple", period: "Three Months", frequency: 100 }
|
|
- { name: "Coupang", period: "Three Months", frequency: 100 }
|
|
- { name: "IBM", period: "Three Months", frequency: 100 }
|
|
- { name: "MongoDB", period: "Three Months", frequency: 100 }
|
|
- { name: "Atlassian", period: "Three Months", frequency: 89.6 }
|
|
- { name: "Salesforce", period: "Three Months", frequency: 88 }
|
|
- { name: "Yandex", period: "Three Months", frequency: 86.7 }
|
|
- { name: "Visa", period: "Three Months", frequency: 80.9 }
|
|
- { name: "Goldman Sachs", period: "Three Months", frequency: 57 }
|
|
- { name: "American Express", period: "Six Months", frequency: 100 }
|
|
- { name: "Expedia", period: "Six Months", frequency: 100 }
|
|
- { name: "General Motors", period: "Six Months", frequency: 100 }
|
|
- { name: "Nextdoor", period: "Six Months", frequency: 100 }
|
|
- { name: "Ozon", period: "Six Months", frequency: 100 }
|
|
- { name: "Ripple", period: "Six Months", frequency: 100 }
|
|
- { name: "Tesco", period: "Six Months", frequency: 100 }
|
|
- { name: "Morgan Stanley", period: "Six Months", frequency: 83.1 }
|
|
- { name: "Walmart Labs", period: "Six Months", frequency: 81.4 }
|
|
- { name: "Tesla", period: "Six Months", frequency: 80.9 }
|
|
- { name: "Zoho", period: "Six Months", frequency: 79.6 }
|
|
- { name: "eBay", period: "Six Months", frequency: 75.2 }
|
|
- { name: "TikTok", period: "Six Months", frequency: 64.9 }
|
|
- { name: "Capital One", period: "Six Months", frequency: 64.2 }
|
|
- { name: "Uber", period: "Six Months", frequency: 55.2 }
|
|
- { name: "LinkedIn", period: "Six Months", frequency: 54.1 }
|
|
sidebar:
|
|
label: 'Merge Intervals'
|
|
badge: 'Medium'
|
|
---
|
|
|
|
### Example 1:
|
|
- Input: `intervals = [[1,3],[2,6],[8,10],[15,18]]`
|
|
- Output: `[[1,6],[8,10],[15,18]]`
|
|
- Explanation: Since `intervals [1,3]` and `[2,6]` overlap, merge them into `[1,6]`.
|
|
|
|
### Example 2:
|
|
- Input: `intervals = [[1,4],[4,5]]`
|
|
- Output: `[[1,5]]`
|
|
- Explanation: Intervals `[1,4]` and `[4,5]` are considered overlapping.
|
|
|
|
### Example 3:
|
|
- Input: `intervals = [[4,7],[1,4]]`
|
|
- Output: `[[1,7]]`
|
|
- Explanation: Intervals `[1,4]` and `[4,7]` are considered overlapping.
|
|
|
|
### Constraints:
|
|
|
|
- `1 <= intervals.length <= 10^4`
|
|
- `intervals[i].length == 2`
|
|
- `0 <= starti <= endi <= 10^4`
|
|
|
|
## Solution
|
|
|
|
```py
|
|
class Solution:
|
|
def merge(self, intervals: list[list[int]]) -> list[list[int]]:
|
|
intervals.sort(key=lambda x: x[0])
|
|
result = []
|
|
|
|
for start, end in intervals:
|
|
if result and start <= result[-1][1]:
|
|
result[-1][1] = max(result[-1][1], end)
|
|
else:
|
|
result.append([start, end])
|
|
|
|
return result
|
|
```
|