File tree Expand file tree Collapse file tree 5 files changed +177
-1
lines changed
1452-People-Whose-List-of-Favorite-Companies-Is-Not-a-Subset-of-Another-List/cpp-1452 Expand file tree Collapse file tree 5 files changed +177
-1
lines changed Original file line number Diff line number Diff line change
1
+ cmake_minimum_required (VERSION 3.16 )
2
+ project (C )
3
+
4
+ set (CMAKE_CXX_STANDARD 14 )
5
+
6
+ add_executable (C main3.cpp )
Original file line number Diff line number Diff line change
1
+ // / Source : https://leetcode.com/problems/people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list/
2
+ // / Author : liuyubobobo
3
+ // / Time : 2020-05-16
4
+
5
+ #include < iostream>
6
+ #include < vector>
7
+ #include < unordered_set>
8
+
9
+ using namespace std ;
10
+
11
+
12
+ // / Brute Force + HashSet to check subset
13
+ // / Time Complexity: O(n^2 * m * |s|)
14
+ // / Space Complexity: O(n * m)
15
+ class Solution {
16
+ public:
17
+ vector<int > peopleIndexes (vector<vector<string>>& favoriteCompanies) {
18
+
19
+ int n = favoriteCompanies.size ();
20
+
21
+ vector<unordered_set<string>> setv;
22
+ for (const vector<string>& v: favoriteCompanies)
23
+ setv.push_back (unordered_set<string>(v.begin (), v.end ()));
24
+
25
+ vector<int > res;
26
+ for (int i = 0 ; i < n; i ++){
27
+ bool is_sub = false ;
28
+ for (int j = 0 ; j < n; j ++)
29
+ if (j != i && setv[i].size () < setv[j].size () && isSub (setv[i], setv[j])){
30
+ is_sub = true ;
31
+ break ;
32
+ }
33
+
34
+ if (!is_sub) res.push_back (i);
35
+ }
36
+ return res;
37
+ }
38
+
39
+ private:
40
+ // see if a is subset of b
41
+ bool isSub (const unordered_set<string>& a, const unordered_set<string>& b){
42
+
43
+ for (string e: a)
44
+ if (!b.count (e)) return false ;
45
+ return true ;
46
+ }
47
+ };
48
+
49
+
50
+ int main () {
51
+
52
+ return 0 ;
53
+ }
Original file line number Diff line number Diff line change
1
+ // / Source : https://leetcode.com/problems/people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list/
2
+ // / Author : liuyubobobo
3
+ // / Time : 2020-05-16
4
+
5
+ #include < iostream>
6
+ #include < vector>
7
+ #include < unordered_set>
8
+
9
+ using namespace std ;
10
+
11
+
12
+ // / Brute Force + Binary Search to check subset
13
+ // / Time Complexity: O(n^2 * m * logm * |s|)
14
+ // / Space Complexity: O(n * m)
15
+ class Solution {
16
+ public:
17
+ vector<int > peopleIndexes (vector<vector<string>>& favoriteCompanies) {
18
+
19
+ int n = favoriteCompanies.size ();
20
+ for (int i = 0 ; i < n; i ++)
21
+ sort (favoriteCompanies[i].begin (), favoriteCompanies[i].end ());
22
+
23
+ vector<int > res;
24
+ for (int i = 0 ; i < n; i ++){
25
+ bool is_sub = false ;
26
+ for (int j = 0 ; j < n; j ++)
27
+ if (j != i && favoriteCompanies[i].size () < favoriteCompanies[j].size () &&
28
+ isSub (favoriteCompanies[i], favoriteCompanies[j])){
29
+ is_sub = true ;
30
+ break ;
31
+ }
32
+
33
+ if (!is_sub) res.push_back (i);
34
+ }
35
+ return res;
36
+ }
37
+
38
+ private:
39
+ // see if a is subset of b
40
+ bool isSub (const vector<string>& a, const vector<string>& b){
41
+
42
+ for (string e: a){
43
+ vector<string>::const_iterator iter = lower_bound (b.begin (), b.end (), e);
44
+ if (iter == b.end () || *iter != e) return false ;
45
+ }
46
+ return true ;
47
+ }
48
+ };
49
+
50
+
51
+ int main () {
52
+
53
+ return 0 ;
54
+ }
Original file line number Diff line number Diff line change
1
+ // / Source : https://leetcode.com/problems/people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list/
2
+ // / Author : liuyubobobo
3
+ // / Time : 2020-05-16
4
+
5
+ #include < iostream>
6
+ #include < vector>
7
+ #include < unordered_set>
8
+ #include < unordered_map>
9
+
10
+ using namespace std ;
11
+
12
+
13
+ // / Brute Force + Using String to Int Set to check subset
14
+ // / Time Complexity: O(n * m * |s| + n^2 * m)
15
+ // / Space Complexity: O(n * m)
16
+ class Solution {
17
+ public:
18
+ vector<int > peopleIndexes (vector<vector<string>>& favoriteCompanies) {
19
+
20
+ int n = favoriteCompanies.size ();
21
+
22
+ unordered_map<string, int > map;
23
+ int index = 0 ;
24
+
25
+ vector<unordered_set<int >> mapv;
26
+ for (const vector<string>& v: favoriteCompanies){
27
+ unordered_set<int > tv;
28
+ for (const string& s: v){
29
+ if (!map.count (s)) map[s] = index ++;
30
+ tv.insert (map[s]);
31
+ }
32
+ mapv.push_back (tv);
33
+ }
34
+
35
+ vector<int > res;
36
+ for (int i = 0 ; i < n; i ++){
37
+ bool is_sub = false ;
38
+ for (int j = 0 ; j < n; j ++)
39
+ if (j != i && mapv[i].size () <= mapv[j].size () && isSub (mapv[i], mapv[j])){
40
+ is_sub = true ;
41
+ break ;
42
+ }
43
+
44
+ if (!is_sub) res.push_back (i);
45
+ }
46
+ return res;
47
+ }
48
+
49
+ private:
50
+ // see if a is subset of b
51
+ bool isSub (const unordered_set<int >& a, const unordered_set<int >& b){
52
+
53
+ for (int e: a)
54
+ if (!b.count (e)) return false ;
55
+ return true ;
56
+ }
57
+ };
58
+
59
+
60
+ int main () {
61
+
62
+ return 0 ;
63
+ }
Original file line number Diff line number Diff line change @@ -1052,7 +1052,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
1052
1052
| 1449 | [ Form Largest Integer With Digits That Add up to Target] ( https://leetcode.com/problems/form-largest-integer-with-digits-that-add-up-to-target/ ) | [ 无] | [ C++] ( 1449-Form-Largest-Integer-With-Digits-That-Add-up-to-Target/cpp-1449/ ) | | |
1053
1053
| 1450 | [ Number of Students Doing Homework at a Given Time] ( https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/ ) | [ 无] | [ C++] ( 1450-Number-of-Students-Doing-Homework-at-a-Given-Time/cpp-1450/ ) | | |
1054
1054
| 1451 | [ Rearrange Words in a Sentence] ( https://leetcode.com/problems/rearrange-words-in-a-sentence/ ) | [ 无] | [ C++] ( 1451-Rearrange-Words-in-a-Sentence/cpp-1451/ ) | | |
1055
- | | | | | | |
1055
+ | 1452 | [ People Whose List of Favorite Companies Is Not a Subset of Another List ] ( https://leetcode.com/problems/people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list/ ) | [ 无 ] | [ C++ ] ( 1452-People-Whose-List-of-Favorite-Companies-Is-Not-a-Subset-of-Another-List/cpp-1452/ ) | | |
1056
1056
1057
1057
## 力扣中文站比赛
1058
1058
You can’t perform that action at this time.
0 commit comments