Today’s problem is challenging — a classic problem you may have seen in your algorithms courses. For those of you who’ve already seen it, this will serve as a great comprehensive review. If it’s new to you, take your time to digest it all.

Let’s take a look at the problem.

We have `n`

jobs, where every job is scheduled to be done from `startTime[i]`

to `endTime[i]`

, obtaining a profit of `profit[i]`

.

You’re given the `startTime`

, `endTime`

and `profit`

arrays. You need to output the maximum profit you can take, such that there are no two jobs in the subset with an overlapping time range.

If you choose a job that ends at time x you will be able to start another job that starts at time x.

- Input:
`startTime`

= [1,2,3,3],`endTime`

= [3,4,5,6],`profit`

= [50,10,40,70] - Output: 120
- Explanation: The subset chosen is the first and fourth job.
- Time range [1–3]+[3–6] , we get profit of 120 = 50 + 70.

- Input:
`startTime`

= [1,2,3,4,6],`endTime`

= [3,5,10,6,9],`profit`

= [20,20,100,70,60] - Output: 150
- Explanation: The subset chosen is the first, fourth and fifth job.
- Profit obtained 150 = 20 + 70 + 60.

Input: `startTime`

= [1,1,1], `endTime`

= [2,3,4], `profit`

= [5,6,4]

Output: 6

Before we get into solving this problem, let’s consider a simpler version of it.

Suppose the profit for all jobs is equal. Essentially, this means that completing as many jobs as possible will be our optimal solution (it really doesn’t matter what job you take as long as it’s a job). Does this sound familiar? Bingo — it sounds like a greedy algorithm type of problem!

#interview #software-development #interview-questions #python #programming

14.15 GEEK