A print shop takes orders that each need exactly one full day on its only press. Every order has a deadline: it earns its profit only if it is printed on or before that day. Orders that cannot be fitted in are turned away and earn nothing. Days are numbered from 1, and the press starts on day 1.
Each job is an object { id, deadline, profit }. Write two functions that take the array jobs:
maxJobProfit(jobs) returns { count, profit }: the largest total profit the shop can earn, and how many jobs that schedule runs.
planJobs(jobs) returns the ids of the jobs in one best schedule, in the order they run. The job at position i runs on day i + 1, so its deadline must be at least i + 1.
Rules:
id is a string and ids are unique. deadline and profit are positive safe integers. If jobs is not an array, or any job breaks these rules (including a repeated id), throw a TypeError.
Several schedules can earn the same best profit. planJobs may return any of them; the tests check that the schedule is valid and earns the maximum. Because every profit is positive, every best schedule runs the same number of jobs, so count is well defined.
An empty list earns { count: 0, profit: 0 } and has the plan [].
Do not modify jobs.
There can be 100,000 jobs, with deadlines up to 10 ** 9. Searching day by day for a free slot is quadratic when many jobs share a late deadline; aim for O(n log n).