Maximum Profit in Job Scheduling

2019-10-21

This is my solution for the LeetCode problem number 1235, Maximum Profit in Job Scheduling.

This problem is quite similar to that of determining the maximum number of not overlapping ranges. The only difference is that in this problem the ranges have a profit associated with them, rather than a linear function over the size of the range.

The key idea for my solution, which runs in , is to sort all jobs by their starting time and, going backwards, store the result of the maximum profit when starting at time . For every job that starts at , the maximum when starting at has to be set to either the maximum found for any or the maximum for any , where is the ending time of the job , plus the profit of job , whichever is bigger. This is the optimization of the choice between scheduling job or skipping it.

struct Job {
  int startTime;
  int endTime;
  int profit;

  Job(int startTime, int endTime, int profit) : startTime(startTime), endTime(endTime), profit(profit) {}

  bool operator<(const Job& rhs) const {
    if (startTime < rhs.startTime) return true;
    if (rhs.startTime < startTime) return false;
    if (endTime < rhs.endTime) return true;
    if (rhs.endTime < endTime) return false;
    return profit < rhs.profit;
  }
};

int firstEqualOrGreater(const map<int, int>& bestStartingFrom, int key) {
  return bestStartingFrom.upper_bound(key - 1)->second;
}

int jobScheduling(vector<int>& startTime, vector<int>& endTime, vector<int>& profit) {
  vector<Job> jobs;
  for (size_t i = 0; i < startTime.size(); i++) {
    jobs.emplace_back(startTime[i], endTime[i], profit[i]);
  }
  // Sorting this vector is O(n lg n).
  sort(begin(jobs), end(jobs));
  map<int, int> bestStartingFrom;
  bestStartingFrom[numeric_limits<int>::max()] = 0;
  // Querying and inserting n times here is O(n lg n).
  for (auto it = rbegin(jobs); it != rend(jobs); it++) {
    const auto a = firstEqualOrGreater(bestStartingFrom, it->startTime);
    const auto b = it->profit + firstEqualOrGreater(bestStartingFrom, it->endTime);
    bestStartingFrom[it->startTime] = max(a, b);
  }
  return firstEqualOrGreater(bestStartingFrom, 0);
}