RainX_69

INTUIT OA PROBLEM BASED ON DP+BITMASK

Feb 10th, 2023 (edited)
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | Source Code | 0 0
  1. Question in my drive -> https://drive.google.com/drive/folders/1EQL2_ZqDflLZ7g18H4tGpusxwh2-WBpJ?usp=sharing
  2.  
  3. Similar question -> https://leetcode.com/problems/smallest-sufficient-team/
  4.  
  5. --------------------------------------------------------------------------------------------------------------------------------------
  6.  
  7. #include<bits/stdc++.h>
  8. using namespace std;
  9.  
  10. int dp[(1<<16)+1];
  11. unordered_map<int,unordered_set<int>> mpp;  // book -> skill
  12. vector<vector<int>> books;
  13. vector<int> req_skill;
  14. int n,m;
  15.    
  16. int helper(int mask, int currBook){
  17.     if(mask==(1<<n)-1){ // if every skill is acquired
  18.         return 0;
  19.     }
  20.    
  21.     if(currBook==m){
  22.         return INT_MAX/2;
  23.     }
  24.            
  25.     if(dp[mask]!=-1){
  26.        return dp[mask];
  27.     }
  28.        
  29.     int IGNORE=helper(mask,currBook+1); // IGNORE CURRENT SKILL BOOK
  30.  
  31.     int ACCEPT=INT_MAX/2; // ACCEPT CURRENT SKILL BOOK    
  32.     bool newSkillAcquired=false;
  33.     int newMask=mask;
  34.     for(int skill=0;skill<n;skill++){
  35.         if(newMask & (1<<skill)){  // already this skill is acquired
  36.             continue;
  37.         }
  38.         if(mpp[currBook].find(req_skill[skill]-1)==mpp[currBook].end()){  // skill not present in book
  39.             continue;
  40.         }
  41.         newMask=newMask | (1<<skill);
  42.         newSkillAcquired=true;
  43.     }
  44.     if(newSkillAcquired==true){
  45.         ACCEPT=books[currBook][0]+helper(newMask,currBook+1);  
  46.     }
  47.    
  48.     return dp[mask]=min(IGNORE,ACCEPT);
  49. }
  50.    
  51. int minCost(vector<int> &req_skills, vector<int> &alice, vector<vector<int>> &book) {
  52.     n=req_skills.size();
  53.     m=book.size();
  54.    
  55.     books=book;
  56.     req_skill=req_skills;
  57.    
  58.     int mask=0;
  59.     for(int bk=0;bk<book.size();bk++){
  60.         for(int skill=1;skill<book[bk].size();skill++){
  61.             mpp[bk].insert(books[bk][skill]-1);
  62.         }
  63.     }
  64.    
  65.     unordered_set<int> rsk;
  66.     for(auto req: req_skills){
  67.         rsk.insert(req-1);
  68.     }
  69.    
  70.     for(auto alice_skill: alice){
  71.         if(rsk.find(alice_skill-1)!=rsk.end()){  // alice has the required skill
  72.             mask=mask | (1 << (alice_skill-1));  
  73.         }
  74.     }
  75.    
  76.     memset(dp,-1,sizeof(dp));
  77.     int res=helper(mask,0);  
  78.     return res==INT_MAX/2 ? -1 : res;
  79. }
  80.  
  81. int main(){
  82.     vector<int> skill={1,2,3,4};
  83.     vector<int> alice={1,5};
  84.     vector<vector<int>> books={{40,2,3,5},{30,4,4},{60,2,3,4}};
  85.     cout<<minCost(skill,alice,books);
  86. }
  87.  
  88.  
  89.  
  90. You can instead prefind the masks for each book, that way you would not need to calculate again and again in recursion. See the leetcode problem to know what I am actually talking about
Advertisement
Add Comment
Please, Sign In to add comment