Member-only story
Find Maximum Profit in Job Scheduling in JavaScript
I recently came across an interview question posted on Geeks For Geeks! Even though questions might not be the same, the first rounds are usually coding tests, and it's always good to have some practice done on such dynamic programming questions.
Given three arrays called pickup, drop, and tip. Find the max profit the delivery guy can earn. The guy can only process one delivery at a time.
Example: He gets profit of 5 - 0 + 1 = 6
6 units if the 1st order is delivered
pickup: [0,2,9,10,11,12]
drop:[5,9,11,11,14,17]
tip:[1,2,3,2,2,1]
This program looks like a variation of ‘Maximum Profit in Job Scheduling’ :
We have three arrays, start
, end
, profit
for N
jobs. Then, the program should return the maximum profit when no two jobs overlap the same time range, i.e., every scheduled job is to be done from start[i]
to end[i]
with a profit of profit[i]
.
Example:Input: start = [2, 3, 4, 6, 7], end = [4,6,11,7,10] and profit =[30,30,200,80,70]Output: 180How?: The elements chosen are the first, fourth, and fifth jobs.
Thus, we will get the profit of 180 = 30 + 80 + 70.
Implementation using JavaScript: