Search code examples
phpalgorithmlogicmaximum-profit-problem

Interval Scheduling Algorithm or Activity Selection Algorithm


I'm struggling with this question for so long.

There are n persons who want same room in a hotel. Each person wants to stay in the hotel for his own convenient time but only one person can stay at a time. Assume that room is available from 5AM to 11PM. Hotel manager takes 500 rupees from each person who is staying in that room. It does not matter how long a person stays in that room. We have to maximize the profit of the manager. Let us say that n =4 i.e. four persons want same room. Let us say that 1st person wants the room from 6AM to 8AM and 2nd person wants room from 7AM to 8AM, 3rd person wants room from 8AM to 12PM and 4th person wants room from 11AM to 1PM.

enter image description here

By observing above figure, we can easily see that manager can only allow maximum of two persons to stay (1st and 3rd or 1st and 4th or 2nd and 3rd or 2nd and 4th). So maximum profit he can get is 500+500 = 1000 rupees. So we have to implement an algorithm which can find maximum profit value. Assume that the persons want the room only b/w 5AM to 11PM and each person wants room in multiple of hours.

Input description:

{<1st person starting time>#<1st person ending time>,<2nd person starting time>#<2nd person ending time>,…………, # }

Output description:

Output should be maximum profit value.
For the example considered in the question, output is 2000.

Example:

Input:
{6AM#8AM,11AM#1PM,7AM#3PM,7AM#10AM,10AM#12PM,2PM#4PM,1PM#4PM,8AM#9AM}

Output:
2000


Solution

  • Looks like a variant of the Activity Selection Problem.

    Read this TopCoder Tutorial for an excellent explanation.