Assuming you're a freelance contractor and only take on 1 contract at a time, come up with an algorithm that chooses the most profitable non-overlapping jobs from the list below:
potential_contract_jobs = [
{
"start_day": 1,
"end_day": 5,
"profit": 100
},
{
"start_day": 5,
"end_day": 6,
"profit": 10
},
{
"start_day": 15,
"end_day": 20,
"profit": 500
},
{
"start_day": 10,
"end_day": 10,
"profit": 150
},
{
"start_day": 9,
"end_day": 12,
"profit": 20
},
{
"start_day": 11,
"end_day": 12,
"profit": 796
}
]
Additional Info:
I saw a this question posted as a potential job interview question and I think it's a great question but it's completely stumped me.
The approach I've tried (and failed) started similar to this:
- Iterate the list and automatically select any jobs w/o overlaps
- Sort the list of jobs w/ overlaps by profit
- Start accepting them in order of profitability
This fails once you address the idea that in a given range of time multiple jobs might be more profitable than taking 1 long one...
I'd love to find a solution, even if it's pseudo code.
Thank you