📖 Scenario: You are organizing a schedule with several time intervals. Some intervals overlap, and you want to remove the minimum number of intervals so that no intervals overlap.Think of it like planning meetings in a room where no two meetings can happen at the same time.
🎯 Goal: Build a TypeScript program that finds the minimum number of intervals to remove to make the rest non-overlapping.
📋 What You'll Learn
Create an array of intervals where each interval is a tuple of two numbers representing start and end times.
Add a variable to count the number of intervals removed.
Implement logic to sort intervals and find overlaps.
Print the minimum number of intervals to remove.
💡 Why This Matters
🌍 Real World
Scheduling meetings, booking rooms, or managing tasks where overlapping times cause conflicts.
💼 Career
This problem is common in software engineering roles involving scheduling, calendar apps, and resource allocation.
Progress0 / 4 steps
1
Create the intervals array
Create a variable called intervals and assign it the array of intervals: [[1, 3], [2, 4], [3, 5], [6, 7]].
DSA Typescript
Hint
Use const intervals: [number, number][] to declare the array of intervals.
2
Add a variable to count removals
Add a variable called removeCount and set it to 0 to count how many intervals we remove.
DSA Typescript
Hint
Use let removeCount = 0; to start counting removals.
3
Sort intervals and find overlaps
Sort the intervals array by the end time of each interval using intervals.sort((a, b) => a[1] - b[1]). Then, create a variable prevEnd set to the end time of the first interval. Use a for loop starting from index 1 with variable i to check if the start time of intervals[i] is less than prevEnd. If yes, increment removeCount. Otherwise, update prevEnd to intervals[i][1].
DSA Typescript
Hint
Sort by end time to keep intervals that finish earliest. Then check overlaps by comparing start time with previous end time.
4
Print the minimum number of intervals to remove
Print the value of removeCount using console.log(removeCount).
DSA Typescript
Hint
Use console.log(removeCount) to show the result.
Practice
(1/5)
1. What is the main goal of the Non Overlapping Intervals Minimum Removal problem?
easy
A. To remove the minimum number of intervals so that the remaining intervals do not overlap
B. To find the maximum number of overlapping intervals
C. To merge all intervals into one large interval
D. To sort intervals by their start time
Solution
Step 1: Understand the problem goal
The problem asks to remove as few intervals as possible so that no two intervals overlap.
Step 2: Clarify what non-overlapping means
Non-overlapping means intervals do not share any common points except possibly touching ends.
Final Answer:
To remove the minimum number of intervals so that the remaining intervals do not overlap -> Option A
Quick Check:
Minimum removal = non-overlapping intervals [OK]
Hint: Focus on removing intervals, not merging or counting overlaps [OK]
Common Mistakes:
Thinking the goal is to merge intervals
Confusing maximum overlap with minimum removal
Sorting intervals without removing any
2. Which of the following TypeScript function signatures correctly represents a function to solve the Non Overlapping Intervals Minimum Removal problem?
easy
A. function eraseOverlapIntervals(intervals: string[][]): string
B. function eraseOverlapIntervals(intervals: number[]): number[]
C. function eraseOverlapIntervals(intervals: number[][]): number
D. function eraseOverlapIntervals(intervals: number[][]): void
Solution
Step 1: Identify input and output types
The input is an array of intervals, each interval is an array of two numbers. The output is the count of intervals removed, a number.
Step 2: Match the correct signature
function eraseOverlapIntervals(intervals: number[][]): number matches input as number[][] and output as number, which fits the problem.
Final Answer:
function eraseOverlapIntervals(intervals: number[][]): number -> Option C
Quick Check:
Input array of number arrays, output number [OK]
Hint: Input is array of number pairs, output is a number count [OK]
Common Mistakes:
Using wrong input type like number[] instead of number[][]
Returning array instead of number
Using void return type
3. What is the output of the following TypeScript code?
function eraseOverlapIntervals(intervals: number[][]): number {
intervals.sort((a, b) => a[1] - b[1]);
let count = 0;
let end = -Infinity;
for (const interval of intervals) {
if (interval[0] >= end) {
end = interval[1];
} else {
count++;
}
}
return count;
}
console.log(eraseOverlapIntervals([[1,2],[2,3],[3,4],[1,3]]));
Hint: Sort by end, count intervals starting before current end [OK]
Common Mistakes:
Not sorting intervals by end time
Counting intervals incorrectly when start equals end
Confusing count of removals with count of intervals kept
4. Identify the bug in this TypeScript function for minimum interval removal:
function eraseOverlapIntervals(intervals: number[][]): number {
intervals.sort((a, b) => a[0] - b[0]);
let count = 0;
let end = intervals[0][1];
for (let i = 1; i < intervals.length; i++) {
if (intervals[i][0] < end) {
count++;
} else {
end = intervals[i][1];
}
}
return count;
}
medium
A. Initializing end with intervals[0][0] instead of intervals[0][1]
B. Sorting intervals by start time instead of end time causes incorrect removals
C. Loop should start from i = 0 instead of i = 1
D. The function should return the count of intervals kept, not removed
Solution
Step 1: Check sorting criteria
The function sorts intervals by start time, but the greedy approach requires sorting by end time to minimize removals.
Step 2: Understand impact on removal count
Sorting by start time can cause incorrect counting of overlapping intervals, leading to wrong removal count.
Final Answer:
Sorting intervals by start time instead of end time causes incorrect removals -> Option B
Quick Check:
Sort by end time for correct minimum removal [OK]
Hint: Always sort intervals by end time for this problem [OK]
Common Mistakes:
Sorting by start time instead of end time
Wrong initialization of end variable
Incorrect loop start index
5. Given intervals [[0,2],[1,3],[2,4],[3,5],[4,6]], what is the minimum number of intervals to remove to make the rest non-overlapping?