What if you could instantly know the best schedule to fit the most activities without any clashes?
Why Activity Selection Problem in DSA Typescript?
Imagine you have a list of meetings to attend in one day, but some meetings overlap in time. You want to attend as many meetings as possible without any clashes.
Trying to pick meetings manually means checking every meeting against all others to avoid overlaps. This is slow and confusing, especially with many meetings, and easy to make mistakes.
The Activity Selection Problem uses a smart way to pick meetings by sorting them by their finish times and always choosing the earliest finishing meeting next. This ensures you attend the maximum number of meetings without conflicts.
const meetings = [[1,4],[3,5],[0,6],[5,7]]; function overlaps(a: number[], b: number[]): boolean { return a[0] < b[1] && b[0] < a[1]; } // Manually check each meeting against others for overlaps let count = 0; for(let i=0; i<meetings.length; i++) { let canAttend = true; for(let j=0; j<meetings.length; j++) { if(i !== j && overlaps(meetings[i], meetings[j])) { canAttend = false; break; } } if(canAttend) count++; }
const meetings = [[1,4],[3,5],[0,6],[5,7]]; meetings.sort((a,b) => a[1] - b[1]); let count = 1; let lastFinish = meetings[0][1]; for(let i=1; i<meetings.length; i++) { if(meetings[i][0] >= lastFinish) { count++; lastFinish = meetings[i][1]; } }
This approach lets you quickly find the largest number of non-overlapping activities you can do, saving time and avoiding confusion.
Scheduling conference talks to attend the most sessions without missing any due to time clashes.
Manual checking of overlapping activities is slow and error-prone.
Sorting activities by finish time helps pick the maximum number efficiently.
The greedy method ensures the best schedule without conflicts.
Practice
Activity Selection Problem?Solution
Step 1: Understand the problem goal
The Activity Selection Problem aims to choose activities that do not overlap and maximize the count.Step 2: Identify the correct objective
Among the options, only selecting the maximum number of non-overlapping activities matches the problem's goal.Final Answer:
Select the maximum number of activities that don't overlap in time -> Option AQuick Check:
Goal = Max non-overlapping activities [OK]
- Confusing longest duration with maximum count
- Sorting by start time only
- Summing all activity durations
Solution
Step 1: Identify sorting criteria for Activity Selection
Activities must be sorted by their finish times in ascending order to apply the greedy approach.Step 2: Match sorting code to criteria
activities.sort((a, b) => a.finish - b.finish); sorts activities by finish time ascending, which is correct for the problem.Final Answer:
activities.sort((a, b) => a.finish - b.finish); -> Option AQuick Check:
Sort by finish time ascending [OK]
- Sorting by start time instead of finish time
- Sorting finish time descending
- Using incorrect comparison operators
const activities = [
{start: 1, finish: 4},
{start: 3, finish: 5},
{start: 0, finish: 6},
{start: 5, finish: 7},
{start: 8, finish: 9},
{start: 5, finish: 9}
];
activities.sort((a, b) => a.finish - b.finish);
const selected = [];
let lastFinish = 0;
for (const activity of activities) {
if (activity.start >= lastFinish) {
selected.push(activity);
lastFinish = activity.finish;
}
}
console.log(selected);Solution
Step 1: Sort activities by finish time
Sorted list: [{1,4}, {3,5}, {0,6}, {5,7}, {8,9}, {5,9}]Step 2: Select activities greedily
Select {1,4} (start 1 >= 0), then {5,7} (start 5 >= 4), then {8,9} (start 8 >= 7). Others overlap.Final Answer:
[{start:1, finish:4}, {start:5, finish:7}, {start:8, finish:9}] -> Option CQuick Check:
Greedy selection = chosen activities [OK]
- Selecting overlapping activities
- Not sorting by finish time
- Updating lastFinish incorrectly
const activities = [
{start: 1, finish: 3},
{start: 2, finish: 5},
{start: 4, finish: 7}
];
activities.sort((a, b) => a.start - b.start);
const selected = [];
let lastFinish = 0;
for (const activity of activities) {
if (activity.start >= lastFinish) {
selected.push(activity);
lastFinish = activity.finish;
}
}
console.log(selected);Solution
Step 1: Check sorting criteria
The code sorts activities by start time, but the greedy algorithm requires sorting by finish time.Step 2: Identify impact of sorting error
Sorting by start time can cause incorrect selection of overlapping activities, breaking the algorithm.Final Answer:
Sorting by start time instead of finish time -> Option BQuick Check:
Sort by finish time needed [OK]
- Sorting by start time
- Confusing variable declarations
- Wrong if condition operators
const activities = [
{start: 0, finish: 3},
{start: 1, finish: 4},
{start: 3, finish: 5},
{start: 4, finish: 7},
{start: 5, finish: 9},
{start: 8, finish: 10}
];How many activities can be selected using the Activity Selection Problem's greedy approach, and which are they?
Solution
Step 1: Sort activities by finish time
Sorted: [{0,3}, {1,4}, {3,5}, {4,7}, {5,9}, {8,10}]Step 2: Select greedily
{0,3} (last=3); skip {1,4}; {3,5} (last=5); {4,7} overlaps with {3,5} so skip; {5,9} (last=9); {8,10} overlaps with {5,9} so skip. Actually, {8,10} starts before 9, so overlaps. So selected are {0,3}, {3,5}, {5,9} only 3 activities.Final Answer:
3 activities: {0,3}, {3,5}, {5,9} -> Option DQuick Check:
Greedy picks non-overlapping max activities [OK]
- Selecting overlapping activities
- Miscounting due to off-by-one errors
- Ignoring sorting step
Ready to prove it?
Pass the test and this pattern moves unknown
