Fastrail08

GFG Job Sequencing Problem. O(N^2) space and time problem | OPTIMISATION POSSIBLE??

Jul 21st, 2025
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.48 KB | None | 0 0
  1. class Solution {
  2.   public:
  3.  
  4.     pair<int, int> getMaxProfit(int level, int timeElapsed, vector<pair<int, int> > &jobs, vector<vector<pair<int, int> > > &memo){
  5.         //BASE CASE HANDLED IMPLICITLY IN LOOP (we never call on invalid levels or out of bounds)
  6.        
  7.         //memo check
  8.         if(memo[level][timeElapsed] != make_pair(-1, -1)){
  9.             return memo[level][timeElapsed];
  10.         }
  11.        
  12.         //maximum profit at current level
  13.         int maxProfit = 0;
  14.         int maxCount = 0;
  15.         //to check if the LOOP ran, if not add profit of current level job EXPLICITLY before returning
  16.         bool flag = false;
  17.        
  18.         for(int i = level + 1; i < jobs.size(); i++){
  19.             //mark flag true
  20.             flag = true;
  21.             if(timeElapsed + 1 < jobs[i].first){
  22.                 // cout << jobs[level].second << "->" << jobs[i].second << '\n';
  23.                 auto res = getMaxProfit(i, timeElapsed + 1, jobs, memo);
  24.                 //found a combination with more profit, so update maxProfit and size of the sequence.
  25.                 if(res.second + jobs[level].second > maxProfit){
  26.                     //adding the profit of current level & adding the current level to the subset
  27.                     maxProfit = jobs[level].second + res.second;
  28.                     maxCount = 1 + res.first;
  29.                 }
  30.             }
  31.         }
  32.        
  33.         //loop didn't run, ADD PROFIT of CURRENT LEVEL JOB EXPLICITLY
  34.         //maxCount = 1 (as the size of the sequence formed level onwards(including level) is 1)
  35.         //no jobs scheduled after the current level, so the profit from recursion starting from level is 0, but the current level job is already scheduled
  36.         //whose profit must be added
  37.         //When the loop doesn't run, we add the value of current level EXPLICITLY (so here values tracked are profit and size)
  38.    
  39.         if(!flag){
  40.             //starting from level, only 1 job scheduled, so the profit of that job and size = 1; is maxProfit and maxCount respectively
  41.             maxProfit = jobs[level].second;
  42.             maxCount = 1;
  43.         }
  44.         return memo[level][timeElapsed] = {maxCount, maxProfit};
  45.     }  
  46.    
  47.  
  48.     vector<int> jobSequencing(vector<int> &deadline, vector<int> &profit) {
  49.         // code here
  50.         vector<pair<int, int> > jobs;
  51.         for(int i = 0; i < deadline.size(); i++){
  52.             jobs.emplace_back(deadline[i], profit[i]);
  53.         }
  54.        
  55.         //ORDER OF JOBS AFTER SORTING
  56.         //jobs[deadline1] < jobs[deadline2], jobs[deadline1] = jobs[deadline2], then jobs[profit1] < jobs[profit2]
  57.         sort(jobs.begin(), jobs.end());
  58.        
  59.         //  for(auto i : jobs){
  60.         //     cout << i.first << " ";
  61.         // }
  62.         // cout << '\n';
  63.         // for(auto i : jobs){
  64.         //     cout << i.second << " ";
  65.         // }
  66.         // cout << '\n';
  67.         int maxProfit = 0;
  68.         int maxCount = 0;
  69.         int count = 0;
  70.         vector<vector<pair<int, int> > > memo(deadline.size(), vector<pair<int, int> >(deadline.size(), {-1, -1}));
  71.         //give every job the chance to be the first in the subset
  72.         for(int i = 0; i < jobs.size(); i++){
  73.             auto iRes = getMaxProfit(i, 0, jobs, memo);
  74.             if(iRes.second > maxProfit){
  75.                 maxProfit = max(maxProfit, iRes.second);
  76.                 maxCount = max(maxCount, iRes.first);
  77.             }
  78.         }
  79.         vector<int> res{maxCount, maxProfit};
  80.         return res;
  81.     }
  82. };
Advertisement
Add Comment
Please, Sign In to add comment