每日一题1125. 最小的必要团队
1.思路
题目链接:https://leetcode.cn/problems/smallest-sufficient-team/
今天的每日一题没有在看题解之前自己做出来,没想到利用状态压缩的办法来做,利用二进制表示当前的状态,这题后续需要二刷
class Solution {
public int[] smallestSufficientTeam(String[] req_skills, List<List<String>> people) {
HashMap<String, Integer> map = new HashMap<>();
for (int i = 0;i< req_skills.length;i++){
map.put(req_skills[i],i );
}
int m = people.size();
int n = req_skills.length;
List<Integer>[] dp = new List[1 << n];
// for(int i = 0;i<dp.length;i++){
// dp[i] = new ArrayList<>();
// }
dp[0] = new ArrayList<>();
for (int i = 0;i<m;i++){
//取出当前people的技能
int skill = 0;
for (String s : people.get(i)){
skill |= 1<<map.get(s);
}
//更新dp
for (int j = 0; j < dp.length; j++) {
if(dp[j]==null) continue;
int new_skill = j| skill;
// System.out.println(skill+" "+j);
// System.out.println(new_skill);
if (dp[new_skill]==null || dp[new_skill].size()>dp[j].size()+1){
dp[new_skill] = new ArrayList<>(dp[j]);
dp[new_skill].add(i);
}
}
}
int[] ints = new int[dp[(1 << n) - 1].size()];
for (int i = 0;i<ints.length;i++){
ints[i] = dp[(1 << n) - 1].get(i);
}
return ints;
}
}