前言
今天虽说题目标签是排序,但是考察的基本是并查集,只要找到并集剔除就可以,为此使用了set的containsAll函数。
题目
源码
class Solution:
def peopleIndexes(self, favoriteCompanies: List[List[str]]) -> List[int]:
favoriteMap=[set(x) for x in favoriteCompanies]
ans=[]
for i,x in enumerate (favoriteMap):
flag=True
for j,y in enumerate(favoriteMap):
if i==j:
continue
if x&y==x:
flag=False
break
if flag:
ans.append(i)
return ans
class Solution {
public List<Integer> peopleIndexes(List<List<String>> favoriteCompanies) {
Map<Integer,Set<String>> map=new HashMap<>();
int len =favoriteCompanies.size();
for(int i=0;i<favoriteCompanies.size();i++){
map.put(i,new HashSet(favoriteCompanies.get(i)));
}
List<Integer> ans = new ArrayList<>();
for(int i=0;i<len;i++){
boolean flag=true;
for(int j=0;j<len;j++){
if(i==j){
continue;
}
if(map.get(j).containsAll(map.get(i))){
flag=false;
break;
}
}
if(flag){
ans.add(i);
}
}
return ans;
}
}