<p>In a project, you have a list of required skills <code>req_skills</code>, and a list of people. The <code>i<sup>th</sup></code> person <code>people[i]</code> contains a list of skills that the person has.</p>

<p>Consider a sufficient team: a set of people such that for every required skill in <code>req_skills</code>, there is at least one person in the team who has that skill. We can represent these teams by the index of each person.</p>

<ul>
	<li>For example, <code>team = [0, 1, 3]</code> represents the people with skills <code>people[0]</code>, <code>people[1]</code>, and <code>people[3]</code>.</li>
</ul>

<p>Return <em>any sufficient team of the smallest possible size, represented by the index of each person</em>. You may return the answer in <strong>any order</strong>.</p>

<p>It is <strong>guaranteed</strong> an answer exists.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
<strong>Output:</strong> [0,2]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
<strong>Output:</strong> [1,2]
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= req_skills.length &lt;= 16</code></li>
	<li><code>1 &lt;= req_skills[i].length &lt;= 16</code></li>
	<li><code>req_skills[i]</code> consists of lowercase English letters.</li>
	<li>All the strings of <code>req_skills</code> are <strong>unique</strong>.</li>
	<li><code>1 &lt;= people.length &lt;= 60</code></li>
	<li><code>0 &lt;= people[i].length &lt;= 16</code></li>
	<li><code>1 &lt;= people[i][j].length &lt;= 16</code></li>
	<li><code>people[i][j]</code> consists of lowercase English letters.</li>
	<li>All the strings of <code>people[i]</code> are <strong>unique</strong>.</li>
	<li>Every skill in <code>people[i]</code> is a skill in <code>req_skills</code>.</li>
	<li>It is guaranteed a sufficient team exists.</li>
</ul>

class Solution {
    vector<vector<int>> dp;
    int n;
    
    vector<int> minPeople(int mask, 
                          vector<bool>& peopleVis,
                          map<int, vector<int>>& skillToPeopleMap, 
                          map<int, vector<int>>& peopleToSkillMap) {
        // cout << mask << endl;
        if(mask == ((1 << n) - 1)) {
            return {};
        }
        
        if(!dp[mask].empty()) {
            return dp[mask];
        }
        
        vector<int> result;
        int initSize = INT_MAX;
        
        for(int i = 0; i < n; ++i) {
            if((mask & (1 << i)) == 0) {
                for(auto people: skillToPeopleMap[i]) {
                    if(!peopleVis[people]) {
                        int newMask = mask;
                        peopleVis[people] = true;

                        for(auto skill: peopleToSkillMap[people]) {
                            newMask |= (1 << skill);
                        }

                        vector<int> temp = minPeople(newMask, peopleVis, skillToPeopleMap, peopleToSkillMap);
                        temp.push_back(people);
                        
                        if(temp.size() < initSize) {
                            initSize = temp.size();
                            result = temp;
                        }
                        peopleVis[people] = false;
                    }
                }
            }
        }
        return dp[mask] = result;
    }
    
    
public:
    vector<int> smallestSufficientTeam(vector<string>& req_skills, vector<vector<string>>& people) {
        
        n = req_skills.size();
        dp.resize(1<<n);
        
        map<int, vector<int>> skillToPeopleMap;
        map<int, vector<int>> peopleToSkillMap;
        map<string, int> skillsToIndex;
        for(int i = 0; i < n; ++i) {
            skillsToIndex[req_skills[i]] = i;
        }
        for(int i = 0; i < people.size(); ++i) {
            for(auto& skill : people[i]) {
                skillToPeopleMap[skillsToIndex[skill]].push_back(i);              
                peopleToSkillMap[i].push_back(skillsToIndex[skill]);
            }
        }
        vector<bool> peopleVis(people.size(), false);
        
        return minPeople(0, peopleVis, skillToPeopleMap, peopleToSkillMap);
    }
};
190526