- Define precisely what one DP state represents.
- Establish the base cases before transitions are evaluated.
- Process states in dependency order and combine only already-known values.
Code notes
- 41 lines of C++ from the credited upstream file 1125.cpp.
- The implementation visibly relies on sequence storage, hash lookup, cached states.
- 4 loop blocks detected.
Complexity
Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution {2 public:3 vector<int> smallestSufficientTeam(vector<string>& req_skills,4 vector<vector<string>>& people) {5 const int n = req_skills.size();6 const int nSkills = 1 << n;7 unordered_map<string, int> skillToId;8 9 unordered_map<int, vector<int>> dp;10 dp.reserve(nSkills); 11 dp[0] = {};12 13 for (int i = 0; i < n; ++i)14 skillToId[req_skills[i]] = i;15 16 auto getSkill = [&](const vector<string>& person) {17 int mask = 0;18 for (const string& skill : person)19 if (const auto it = skillToId.find(skill); it != skillToId.cend())20 mask |= 1 << it->second;21 return mask;22 };23 24 for (int i = 0; i < people.size(); ++i) {25 const int currSkill = getSkill(people[i]);26 for (const auto& [mask, indices] : dp) {27 const int newSkillSet = mask | currSkill;28 if (newSkillSet == mask) 29 continue;30 if (!dp.contains(newSkillSet) ||31 dp[newSkillSet].size() > indices.size() + 1) {32 dp[newSkillSet] = indices;33 dp[newSkillSet].push_back(i);34 }35 }36 }37 38 return dp[nSkills - 1];39 }40};41