79.8k views
4 votes
Suppose you are a cable guy and are sent out for jobs to complete in a single day. given job ji each has a start time si and finish time fi. Your goal is to complete the most number of jobs assigned. The only requirement is it takes you y minutes to drive between jobs, so you can only start a job y minutes after another one finishes, but no jobs may overlap. Greedy strategy choose the activity that is latest to start that does not over lap with any other activity and you have enough time to drive to. Create the greedy algorithm that implements this strategy and provide its run-time.

1 Answer

2 votes

Final answer:

The greedy algorithm for completing the most number of jobs assigned in a single day as a cable guy involves sorting the jobs based on their start times and selecting the latest job that doesn't overlap with any other job and allows enough time for driving. The runtime of the algorithm is O(n*log(n)).

Step-by-step explanation:

The greedy algorithm for completing the most number of jobs assigned in a single day as a cable guy can be implemented as follows:

  1. Sort the jobs in descending order based on their start times.
  2. Initialize an empty list called 'schedule' to store the selected jobs.
  3. Iterate through the sorted list of jobs:
    1. For each job, check if it overlaps with any job already in the 'schedule' list.
  • If it overlaps, skip the job and continue to the next one.
If it doesn't overlap, check if there is enough time to drive between the previous job in the 'schedule' list and the current job.
  • If there is enough time, add the current job to the 'schedule' list.
Return the 'schedule' list, which contains the selected jobs.

The runtime of this greedy algorithm is O(n*log(n)), where 'n' is the number of jobs. This is because the algorithm involves sorting the jobs based on their start times.

User Mujeeb Ishaque
by
7.4k points