Microsoft - Job Max Profit
I was asked this question during virtual onsite for SDE 1/2 hiring event - not sure what to make of it. Is there an existing question for this on LC?
Input:
List of job ID's ("a", "b", "c", "d" etc)
List of job duration (1, 2, 4, 6, etc)
List of profit you get for scheduling that job (2, 4, 6, 10, etc)
Duration of time you have to schedule the jobs, (e.g. 8)
What is the maximum amount of profit you can make from scheduling jobs?
Each job can only be scheduled at most once.
My intuition was to sort the jobs by how much profit-per-hour they make and then do some kind of backtracking or DP, but I couldn't wrap my head around it. I think that backtracking would work if you have some way to know at what point you will not find a more profitable job combination by continuing the backtracking, but not sure if that is the right line of thinking.
Anybody have any ideas?
Thanks
Edit:
Turns out this is called a '0-1 knapsack problem'.
Thanks to all who commented.