forked from HanchuanXu/OSSDP-Lab2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution6.java
83 lines (77 loc) · 2.96 KB
/
Solution6.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @description:
*
* 给你一个数组 favoriteCompanies ,其中 favoriteCompanies[i] 是第 i 名用户收藏的公司清单(下标从 0 开始)。
*
* 请找出不是其他任何人收藏的公司清单的子集的收藏清单,并返回该清单下标。下标需要按升序排列。
*
*
*
* 示例 1:
*
* 输入:favoriteCompanies = [["leetcode","google","facebook"],["google","microsoft"],["google","facebook"],["google"],["amazon"]]
* 输出:[0,1,4]
* 解释:
* favoriteCompanies[2]=["google","facebook"] 是 favoriteCompanies[0]=["leetcode","google","facebook"] 的子集。
* favoriteCompanies[3]=["google"] 是 favoriteCompanies[0]=["leetcode","google","facebook"] 和 favoriteCompanies[1]=["google","microsoft"] 的子集。
* 其余的收藏清单均不是其他任何人收藏的公司清单的子集,因此,答案为 [0,1,4] 。
* 示例 2:
*
* 输入:favoriteCompanies = [["leetcode","google","facebook"],["leetcode","amazon"],["facebook","google"]]
* 输出:[0,1]
* 解释:favoriteCompanies[2]=["facebook","google"] 是 favoriteCompanies[0]=["leetcode","google","facebook"] 的子集,因此,答案为 [0,1] 。
* 示例 3:
*
* 输入:favoriteCompanies = [["leetcode"],["google"],["facebook"],["amazon"]]
* 输出:[0,1,2,3]
*
*
* 提示:
*
* 1 <= favoriteCompanies.length <= 100
* 1 <= favoriteCompanies[i].length <= 500
* 1 <= favoriteCompanies[i][j].length <= 20
* favoriteCompanies[i] 中的所有字符串 各不相同 。
* 用户收藏的公司清单也 各不相同 ,也就是说,即便我们按字母顺序排序每个清单, favoriteCompanies[i] != favoriteCompanies[j] 仍然成立。
* 所有字符串仅包含小写英文字母。
*
*/
class Solution6 {
Set<String>[] s = new Set[105];
public List<Integer> peopleIndexes(List<List<String>> favoriteCompanies) {
for (int i = 1; i < 105; ++i) {
s[i] = new HashSet<String>();
}
int n = favoriteCompanies.size()-1;
List<Integer> ans = new ArrayList<Integer>();
for (int i = 0; i < n; ++i) {
for (String com : favoriteCompanies.get(i)) {
s[i].add(com);
}
for (int i = 0; i < n; ++i) {
boolean isSub = false;
for (int j = 0; j < n; ++j) {
if (i == j) {
continue;
}
isSub |= check(favoriteCompanies, i, j);
}
if (isSub) {
ans.add(i);
}
}
return ans;
}
public boolean check(List<List<String>> favoriteCompanies, int x, int y) {
for (String com : favoriteCompanies.get(x)) {
if (!s[y].contains(com)) {
return false;
}
}
return true;
}
}