Skip to content

Problem 56 #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ As I work through this list I figure I would make a GitHub repo with my solution
32. [Clone Graph #133](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/clone-graph-133.md)
33. Evaluate Reverse Polish Notation #150
34. Course Schedule #207
35. Implement Trie (Prefix Tree) #208
35. **Implement Trie (Prefix Tree) #208**
36. Coin Change #322
37. Product of Array Except Self #238
38. Min Stack #155
Expand All @@ -53,7 +53,7 @@ As I work through this list I figure I would make a GitHub repo with my solution
42. Search in Rotated Sorted Array #33
43. Combination Sum #39
44. Permutations #46
45. Merge Intervals #56
45. [Merge Intervals #56](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/merge-intervals-56.md)
46. Lowest Common Ancestor of a Binary Tree #236
47. Time Based Key-Value Store #981
48. Accounts Merge #721
Expand All @@ -73,7 +73,7 @@ As I work through this list I figure I would make a GitHub repo with my solution
62. Find All Anagrams in a String #438
63. Minimum Height Trees #310
64. Task Scheduler #621
65. LRU Cache #146
65. **LRU Cache #146**
66. Kth Smallest Element in a BST #230

### Hard
Expand All @@ -85,7 +85,7 @@ As I work through this list I figure I would make a GitHub repo with my solution
71. Word Ladder #127
72. Basic Calculator #224
73. Maximum Profit in Job Scheduling #1235
74. Merge k Sorted Lists #23
74. **Merge k Sorted Lists #23**
75. Largest Rectangle in Histogram #84

## Data Structures
Expand All @@ -105,6 +105,7 @@ In order to practice with similar data structures I'll be placing each problem i
- [Insert Interval #57](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/insert-interval-57.md)
- [01 Matrix #542](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/01-matrix-542.md)
- [3Sum #15](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/3Sum-15.md)
- [Merge Intervals #56](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/merge-intervals-56.md)

### Queue

Expand Down
71 changes: 71 additions & 0 deletions medium/merge-intervals-56.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Merge Intervals

Page on leetcode: https://leetcode.com/problems/merge-intervals/

## Problem Statement

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.

### Constraints

- 1 <= intervals.length <= 104
- intervals[i].length == 2
- 0 <= starti <= endi <= 104

### Example

```
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].
```

```
Input: intervals = [[1,4],[4,5]]
Output: [[1,5]]
Explanation: Intervals [1,4] and [4,5] are considered overlapping.
```

## Solution

- Is intervals always sorted? We will assume yes for now

### Pseudocode

1. Create empty array result
2. Create curInterval and set to intervals[0]
3. Iterate thru intervals
4. ith interval [0] is less than or equal currInterval[1], set curr[0] to min of ith and curr and set curr[1] to max of ith and curr.
5. Else push curr to result
6. Set curr equal to ith
7. Push curr to result
8. Return result

### Initial Solution

This solution has a time complexity of O(nlogn) due to sorting and a space complexity of O(n) due returning a result array. You can see an explanation of the approach to this problem here: https://www.youtube.com/watch?v=44H3cEC2fFM

```javascript
const merge = function (intervals) {
// In order for below to work need a sorted array. Sort lowest to highest by the 0-index of the interval
intervals.sort((a, b) => a[0] - b[0]);

const result = [];
let curr = intervals[0];

for (let i = 1; i < intervals.length; i++) {
if (intervals[i][0] <= curr[1]) {
// If there is overlap, update current interval to span the overlapping intervals
curr[0] = Math.min(curr[0], intervals[i][0]);
curr[1] = Math.max(curr[1], intervals[i][1]);
} else {
// If no overlap then add current interval to the result and update current interval to the just checked interval
result.push(curr);
curr = intervals[i];
}
}
result.push(curr);

return result;
};
```