0
0
DSA Typescriptprogramming~30 mins

Activity Selection Problem in DSA Typescript - Build from Scratch

Choose your learning style9 modes available
Activity Selection Problem
📖 Scenario: You are organizing a conference with many talks. Each talk has a start time and an end time. You want to attend the maximum number of talks without any overlaps.
🎯 Goal: Build a program that selects the maximum number of non-overlapping activities (talks) you can attend.
📋 What You'll Learn
Create an array of activities with start and end times
Sort activities by their end times
Select the maximum number of activities that do not overlap
Print the selected activities in order
💡 Why This Matters
🌍 Real World
Scheduling talks, meetings, or tasks to maximize usage of time without conflicts.
💼 Career
Useful for roles in project management, event planning, and software development involving scheduling algorithms.
Progress0 / 4 steps
1
Create the list of activities
Create an array called activities with these exact objects: { start: 1, end: 4 }, { start: 3, end: 5 }, { start: 0, end: 6 }, { start: 5, end: 7 }, { start: 8, end: 9 }, { start: 5, end: 9 }
DSA Typescript
Hint

Use an array of objects with start and end properties.

2
Sort activities by their end time
Create a new array called sortedActivities by sorting activities in ascending order of their end property using activities.slice().sort((a, b) => a.end - b.end)
DSA Typescript
Hint

Use slice() to copy the array before sorting to keep original intact.

3
Select maximum number of non-overlapping activities
Create an array called selectedActivities and add the first activity from sortedActivities. Then use a for loop with variable i from 1 to sortedActivities.length. Inside the loop, if sortedActivities[i].start is greater or equal to the end of the last selected activity, add sortedActivities[i] to selectedActivities
DSA Typescript
Hint

Start with the first activity and add others only if they start after the last selected activity ends.

4
Print the selected activities
Use console.log to print the selectedActivities array
DSA Typescript
Hint

Print the selectedActivities array to see the chosen talks.