Menu
CoddyTech

Activity Selection Problem

Activity i runs from time start[i] to time finish[i]. One person does one activity at a time, and the next activity must start strictly after the previous one finishes: an activity that starts at the exact time another finishes cannot follow it.

Return the largest number of activities one person can do.

Function

maxActivities(start: integer-array, finish: integer-array) → integer
startinteger-array
the start time of each activity
finishinteger-array
the finish time of each activity; finish[i] belongs to start[i]
Returnsinteger
the most activities one person can do, one after another

Constraints

  • 1 ≤ start.length == finish.length ≤ 5000
  • 0 ≤ start[i] ≤ finish[i] ≤ 105
  • An activity may start and finish at the same time. The arrays are not sorted.

Examples

Input
start = [10, 12, 20, 14, 25, 21]finish = [13, 16, 24, 19, 30, 22]
Output
4
Explanation
Doing 10 to 13, 14 to 19, 21 to 22 and 25 to 30 gives 4. Five is impossible: 10 to 13 clashes with 12 to 16, and 21 to 22 with 20 to 24.
challenge icon

Follow-up

Two people now work in parallel, each doing one activity at a time. How many activities can they do together, and which person should take each one?

Reset code
def maxActivities(start, finish):
    # Write code here

Test #1

Test #2

Test #3

Arguments

start = [10, 12, 20, 14, 25, 21]
finish = [13, 16, 24, 19, 30, 22]

Expected Output

4