Advertisement
nikunjsoni

1235

May 20th, 2021
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int jobScheduling(vector<int>& startTime, vector<int>& endTime, vector<int>& profit) {
  4.         int n = startTime.size();
  5.         vector<vector<int>> jobs;
  6.         for(int i=0; i<n; i++)
  7.             jobs.push_back({endTime[i], startTime[i], profit[i]});
  8.        
  9.         // Sort jobs on end time.
  10.         sort(jobs.begin(), jobs.end());
  11.        
  12.         // Have a dp array where last entry is most profitable.
  13.         map<int, int> dp = {{0,0}};
  14.         for(auto job:jobs){
  15.             int sTime = job[1], eTime = job[0];
  16.             auto pos = --dp.upper_bound(sTime);
  17.             if(pos->second+job[2] > dp.rbegin()->second)
  18.             dp[eTime] = pos->second+job[2];
  19.         }
  20.         return dp.rbegin()->second;
  21.     }
  22. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement