Advertisement
pb_jiang

lc1235

Oct 21st, 2022
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. class Solution
  2. {
  3.   public:
  4.     int jobScheduling(vector<int> &startTime, vector<int> &endTime, vector<int> &profit)
  5.     {
  6.         vector<int> vt;
  7.         for (auto t : startTime)
  8.             vt.push_back(t);
  9.         for (auto t : endTime)
  10.             vt.push_back(t);
  11.         sort(vt.begin(), vt.end());
  12.         vt.erase(unique(vt.begin(), vt.end()), vt.end());
  13.  
  14.         // vector<int> dp(vt.back() + 1);
  15.         vector<int> dp(vt.size() + 1);
  16.  
  17.         vector<vector<int>> times;
  18.         for (int i = 0; i < startTime.size(); ++i) {
  19.             int s = lower_bound(vt.begin(), vt.end(), startTime[i]) - vt.begin() + 1;
  20.             int e = lower_bound(vt.begin(), vt.end(), endTime[i]) - vt.begin() + 1;
  21.             times.push_back({e, s, profit[i]});
  22.         }
  23.         sort(times.begin(), times.end());
  24.  
  25.         for (int i = 1, j = 0; i < dp.size(); ++i) {
  26.             dp[i] = dp[i - 1];
  27.             while (j < times.size() && i >= times[j][0]) {
  28.                 dp[i] = max(dp[i], dp[times[j][1]] + times[j][2]);
  29.                 ++j;
  30.             }
  31.         }
  32.         return dp.back();
  33.     }
  34. };
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement