1

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:

  1. Iterate the list and automatically select any jobs w/o overlaps
  2. Sort the list of jobs w/ overlaps by profit
  3. 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

Billy Blob Snortin
  • 1,101
  • 3
  • 11
  • 17
  • I would suggest to start from here https://en.wikipedia.org/wiki/Job_shop_scheduling – maks Oct 24 '15 at 00:14
  • @maks: That problem isn't really anything like this one. In job shop scheduling, tasks can be run in parallel, all tasks must be completed, and while jobs have fixed lengths, they don't have to take place at fixed times. – user2357112 Oct 24 '15 at 00:18
  • The link above doesn't answer your question directly. It gives you a clue how can you incorporate the ideas in that article to solve your problem – maks Oct 24 '15 at 00:21
  • @maks can you please remove your initial comment? it's not an answer and only adds confusion. thank you! – Billy Blob Snortin Oct 24 '15 at 19:29

1 Answers1

0

For only 6 jobs, you have 2^6 possibilities of choices of jobs. Well feasible to just enumerate. Then check if it is valid, and take the maximum.

Or you can sort by end_day and create a recursive algorithm where the maximum is determined by the maximum of taking the job with profit and be occupied for a while, or not take the job.

Pieter21
  • 1,765
  • 1
  • 10
  • 22