All Problems
6 problems1
Insert Interval
You are given an array of non-overlapping intervals `intervals` where `intervals[i] = [start_i, end_i]` represents the start and the end time of the `ith` interval. `intervals` is initially sorted in ascending order by `start_i`.
You are given another interval `newInterval = [start, end]`.
Insert `newInterval` into `intervals` such that `intervals` is still sorted in ascending order by `start_i` and also `intervals` still does not have any overlapping intervals. You may merge the overlapping intervals if needed.
Return `intervals` after adding `newInterval`.
Note: Intervals are *non-overlapping* if they have no common point. For example, [1,2] and [3,4] are non-overlapping, but [1,2] and [2,3] are overlapping.
**Example 1:**
```java
Input: intervals = [[1,3],[4,6]], newInterval = [2,5]
Output: [[1,6]]
```
**Example 2:**
```java
Input: intervals = [[1,2],[3,5],[9,10]], newInterval = [6,7]
Output: [[1,2],[3,5],[6,7],[9,10]]
```
**Constraints:**
* `0 <= intervals.length <= 1000`
* `newInterval.length == intervals[i].length == 2`
* `0 <= start <= end <= 1000`
<br>
<br>
<details class="hint-accordion">
<summary>Recommended Time & Space Complexity</summary>
<p>
You should aim for a solution with <code>O(n)</code> time and <code>O(1)</code> extra space, where <code>n</code> is the size of the input array.
</p>
</details>
<br>
<details class="hint-accordion">
<summary>Hint 1</summary>
<p>
The given intervals are non-overlapping and sorted in ascending order based on the start value. Try to visualize them as line segments and consider how a new interval can be inserted. Maybe you should analyze different cases of inserting a new interval.
</p>
</details>
<br>
<details class="hint-accordion">
<summary>Hint 2</summary>
<p>
First, we append all intervals to the output list that have an end value smaller than the start value of the new interval. Then, we encounter one of three cases: we have appended all intervals, we reach an interval whose start value is greater than the new interval’s end, or we find an overlapping interval. Can you think of a way to handle these cases efficiently?
</p>
</details>
<br>
<details class="hint-accordion">
<summary>Hint 3</summary>
<p>
We iterate through the remaining intervals, updating the new interval if its end value is greater than or equal to the current interval's start value. We adjust the start and end of the new interval to the minimum and maximum values, respectively. After this, any remaining intervals are appended to the output list, and we return the result.
</p>
</details>
Medium
Not Attempted
Video
2
Merge Intervals
Given an array of `intervals` where `intervals[i] = [start_i, end_i]`, merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.
You may return the answer in **any order**.
Note: Intervals are *non-overlapping* if they have no common point. For example, `[1, 2]` and `[3, 4]` are non-overlapping, but `[1, 2]` and `[2, 3]` are overlapping.
**Example 1:**
```java
Input: intervals = [[1,3],[1,5],[6,7]]
Output: [[1,5],[6,7]]
```
**Example 2:**
```java
Input: intervals = [[1,2],[2,3]]
Output: [[1,3]]
```
**Constraints:**
* `1 <= intervals.length <= 1000`
* `intervals[i].length == 2`
* `0 <= start <= end <= 1000`
<br>
<br>
<details class="hint-accordion">
<summary>Recommended Time & Space Complexity</summary>
<p>
You should aim for a solution as good or better than <code>O(nlogn)</code> time and <code>O(n)</code> space, where <code>n</code> is the size of the input array.
</p>
</details>
<br>
<details class="hint-accordion">
<summary>Hint 1</summary>
<p>
Sorting the given intervals in ascending order based on their start values is beneficial, as it helps in identifying overlapping intervals efficiently. How can you determine if two intervals overlap?
</p>
</details>
<br>
<details class="hint-accordion">
<summary>Hint 2</summary>
<p>
If two intervals are sorted in ascending order by their start values, they overlap if the start value of the second interval is less than or equal to the end value of the first interval.
</p>
</details>
<br>
<details class="hint-accordion">
<summary>Hint 3</summary>
<p>
We iterate through the sorted intervals from left to right, starting with the first interval in the output list. From the second interval onward, we compare each interval with the last appended interval. Can you determine the possible cases for this comparison?
</p>
</details>
<br>
<details class="hint-accordion">
<summary>Hint 4</summary>
<p>
The two cases are: if the current interval overlaps with the last appended interval, we update its end value to the maximum of both intervals' end values and continue. Otherwise, we append the current interval and proceed.
</p>
</details>
Medium
Not Attempted
Video
3
Non-overlapping Intervals
Given an array of intervals `intervals` where `intervals[i] = [start_i, end_i]`, return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.
Note: Intervals are *non-overlapping* even if they have a common point. For example, `[1, 3]` and `[2, 4]` are overlapping, but `[1, 2]` and `[2, 3]` are non-overlapping.
**Example 1:**
```java
Input: intervals = [[1,2],[2,4],[1,4]]
Output: 1
```
Explanation: After [1,4] is removed, the rest of the intervals are non-overlapping.
**Example 2:**
```java
Input: intervals = [[1,2],[2,4]]
Output: 0
```
**Constraints:**
* `1 <= intervals.length <= 1000`
* `intervals[i].length == 2`
* `-50000 <= starti < endi <= 50000`
<br>
<br>
<details class="hint-accordion">
<summary>Recommended Time & Space Complexity</summary>
<p>
You should aim for a solution with <code>O(nlogn)</code> time and <code>O(n)</code> space, where <code>n</code> is the size of the input array.
</p>
</details>
<br>
<details class="hint-accordion">
<summary>Hint 1</summary>
<p>
If two intervals are sorted in ascending order by their start values, they overlap if the start value of the second interval is less than the end value of the first interval. And these are called overlapping intervals.
</p>
</details>
<br>
<details class="hint-accordion">
<summary>Hint 2</summary>
<p>
A brute force approach would be to sort the given intervals in ascending order based on their start values and recursively explore all possibilities. This would be an exponential approach. Can you think of a better way? Maybe a greedy approach works here.
</p>
</details>
<br>
<details class="hint-accordion">
<summary>Hint 3</summary>
<p>
We first sort the given intervals based on their start values to efficiently check for overlaps by comparing adjacent intervals. We then iterate through the sorted intervals from left to right, keeping track of the previous interval’s end value as <code>prevEnd</code>, initially set to the end value of the first interval.
</p>
</details>
<br>
<details class="hint-accordion">
<summary>Hint 4</summary>
<p>
We then iterate from the second interval. If the current interval doesn't overlap, we update <code>prevEnd</code> to the current interval's end and continue. Otherwise, we set <code>prevEnd</code> to the minimum of <code>prevEnd</code> and the current interval’s end, greedily removing the interval that ends last to retain as many intervals as possible.
</p>
</details>
Medium
Not Attempted
Video
4
Meeting Rooms
Given an array of meeting time interval objects consisting of start and end times `[[start_1,end_1],[start_2,end_2],...] (start_i < end_i)`, determine if a person could add all meetings to their schedule without any conflicts.
**Example 1:**
```java
Input: intervals = [(0,30),(5,10),(15,20)]
Output: false
```
Explanation:
* `(0,30)` and `(5,10)` will conflict
* `(0,30)` and `(15,20)` will conflict
**Example 2:**
```java
Input: intervals = [(5,8),(9,15)]
Output: true
```
**Note:**
* (0,8),(8,10) is not considered a conflict at 8
**Constraints:**
* `0 <= intervals.length <= 500`
* `0 <= intervals[i].start < intervals[i].end <= 1,000,000`
<br>
<br>
<details class="hint-accordion">
<summary>Recommended Time & Space Complexity</summary>
<p>
You should aim for a solution with <code>O(nlogn)</code> time and <code>O(n)</code> space, where <code>n</code> is the size of the input array.
</p>
</details>
<br>
<details class="hint-accordion">
<summary>Hint 1</summary>
<p>
If two intervals are sorted in ascending order by their start values, they overlap if the start value of the second interval is less than the end value of the first interval. And these are called overlapping intervals.
</p>
</details>
<br>
<details class="hint-accordion">
<summary>Hint 2</summary>
<p>
A brute force approach would be to check every pair of intervals and return <code>false</code> if any overlap is found. This would be an <code>O(n^2)</code> solution. Can you think of a better way? Maybe you should visualize the given intervals as line segments.
</p>
</details>
<br>
<details class="hint-accordion">
<summary>Hint 3</summary>
<p>
We should sort the given intervals based on their start values, as this makes it easier to check for overlaps by comparing adjacent intervals. We then iterate through the intervals from left to right and return <code>false</code> if any adjacent intervals overlap.
</p>
</details>
Easy
Not Attempted
Video
5
Meeting Rooms II
Given an array of meeting time interval objects consisting of start and end times `[[start_1,end_1],[start_2,end_2],...] (start_i < end_i)`, find the minimum number of days required to schedule all meetings without any conflicts.
**Note:** (0,8),(8,10) is not considered a conflict at 8.
**Example 1:**
```java
Input: intervals = [(0,40),(5,10),(15,20)]
Output: 2
```
Explanation:
day1: (0,40)
day2: (5,10),(15,20)
**Example 2:**
```java
Input: intervals = [(4,9)]
Output: 1
```
**Constraints:**
* `0 <= intervals.length <= 500`
* `0 <= intervals[i].start < intervals[i].end <= 1,000,000`
<br>
<br>
<details class="hint-accordion">
<summary>Recommended Time & Space Complexity</summary>
<p>
You should aim for a solution with <code>O(nlogn)</code> time and <code>O(n)</code> space, where <code>n</code> is the size of the input array.
</p>
</details>
<br>
<details class="hint-accordion">
<summary>Hint 1</summary>
<p>
Try to visualize the meetings as line segments on a number line representing start and end times. The number of rooms required is the maximum number of overlapping meetings at any point on the number line. Can you think of a way to determine this efficiently?
</p>
</details>
<br>
<details class="hint-accordion">
<summary>Hint 2</summary>
<p>
We create two arrays, start and end, containing the start and end times of all meetings, respectively. After sorting both arrays, we use a two-pointer based approach. How do you implement this?
</p>
</details>
<br>
<details class="hint-accordion">
<summary>Hint 3</summary>
<p>
We use two pointers, <code>s</code> and <code>e</code>, for the start and end arrays, respectively. We also maintain a variable <code>count</code> to track the current number of active meetings. At each iteration, we increment <code>s</code> while the start time is less than the current end time and increase <code>count</code>, as these meetings must begin before the earliest ongoing meeting ends.
</p>
</details>
<br>
<details class="hint-accordion">
<summary>Hint 4</summary>
<p>
Then, we increment <code>e</code> and decrement <code>count</code> as a meeting has ended. At each step, we update the result with the maximum value of active meetings stored in <code>count</code>.
</p>
</details>
Medium
Not Attempted
Video
6
Minimum Interval to Include Each Query
You are given a 2D integer array `intervals`, where `intervals[i] = [left_i, right_i]` represents the `ith` interval starting at `left_i` and ending at `right_i` **(inclusive)**.
You are also given an integer array of query points `queries`. The result of `query[j]` is the **length of the shortest interval** `i` such that `left_i <= queries[j] <= right_i`. If no such interval exists, the result of this query is `-1`.
Return an array `output` where `output[j]` is the result of `query[j]`.
Note: The length of an interval is calculated as `right_i - left_i + 1`.
**Example 1:**
```java
Input: intervals = [[1,3],[2,3],[3,7],[6,6]], queries = [2,3,1,7,6,8]
Output: [2,2,3,5,1,-1]
```
Explanation:
- Query = 2: The interval `[2,3]` is the smallest one containing 2, it's length is 2.
- Query = 3: The interval `[2,3]` is the smallest one containing 3, it's length is 2.
- Query = 1: The interval `[1,3]` is the smallest one containing 1, it's length is 3.
- Query = 7: The interval `[3,7]` is the smallest one containing 7, it's length is 5.
- Query = 6: The interval `[6,6]` is the smallest one containing 6, it's length is 1.
- Query = 8: There is no interval containing 8.
**Constraints:**
* `1 <= intervals.length <= 1000`
* `1 <= queries.length <= 1000`
* `1 <= left_i <= right_i <= 10000`
* `1 <= queries[j] <= 10000`
<br>
<br>
<details class="hint-accordion">
<summary>Recommended Time & Space Complexity</summary>
<p>
You should aim for a solution as good or better than <code>O(nlogn + mlogm)</code> time and <code>O(n + m)</code> space, where <code>m</code> is the size of the array <code>queries</code> and <code>n</code> is the size of the array <code>intervals</code>.
</p>
</details>
<br>
<details class="hint-accordion">
<summary>Hint 1</summary>
<p>
A brute force approach would be to iterate through each query and, for each query, check all intervals to find the result. This would be an <code>O(m * n)</code> solution. Can you think of a better way? Maybe processing the queries in sorted order could help.
</p>
</details>
<br>
<details class="hint-accordion">
<summary>Hint 2</summary>
<p>
We sort the intervals by start value and process the queries in ascending order. Using a pointer <code>i</code>, we add intervals to a min-heap while their start values are less than or equal to the query, storing their end values and sizes.
</p>
</details>
<br>
<details class="hint-accordion">
<summary>Hint 3</summary>
<p>
The min-heap is ordered by interval size. We remove elements from the heap while the top element’s end value is less than the current query. The result for the query is the top element’s size if the heap is non-empty; otherwise, it is <code>-1</code>.
</p>
</details>
Hard
Not Attempted
Video
