zoukankan      html  css  js  c++  java
  • [LeetCode] 1452. People Whose List of Favorite Companies Is Not a Subset of Another List

    Given the array favoriteCompanies where favoriteCompanies[i] is the list of favorites companies for the ith person (indexed from 0).

    Return the indices of people whose list of favorite companies is not a subset of any other list of favorites companies. You must return the indices in increasing order.

    Example 1:

    Input: favoriteCompanies = [["leetcode","google","facebook"],["google","microsoft"],["google","facebook"],["google"],["amazon"]]
    Output: [0,1,4] 
    Explanation: 
    Person with index=2 has favoriteCompanies[2]=["google","facebook"] which is a subset of favoriteCompanies[0]=["leetcode","google","facebook"] corresponding to the person with index 0. 
    Person with index=3 has favoriteCompanies[3]=["google"] which is a subset of favoriteCompanies[0]=["leetcode","google","facebook"] and favoriteCompanies[1]=["google","microsoft"]. 
    Other lists of favorite companies are not a subset of another list, therefore, the answer is [0,1,4].
    

    Example 2:

    Input: favoriteCompanies = [["leetcode","google","facebook"],["leetcode","amazon"],["facebook","google"]]
    Output: [0,1] 
    Explanation: In this case favoriteCompanies[2]=["facebook","google"] is a subset of favoriteCompanies[0]=["leetcode","google","facebook"], therefore, the answer is [0,1].
    

    Example 3:

    Input: favoriteCompanies = [["leetcode"],["google"],["facebook"],["amazon"]]
    Output: [0,1,2,3]

    Constraints:

    • 1 <= favoriteCompanies.length <= 100
    • 1 <= favoriteCompanies[i].length <= 500
    • 1 <= favoriteCompanies[i][j].length <= 20
    • All strings in favoriteCompanies[i] are distinct.
    • All lists of favorite companies are distinct, that is, If we sort alphabetically each list then favoriteCompanies[i] != favoriteCompanies[j].
    • All strings consist of lowercase English letters only.

    收藏清单。

    给你一个数组 favoriteCompanies ,其中 favoriteCompanies[i] 是第 i 名用户收藏的公司清单(下标从 0 开始)。

    请找出不是其他任何人收藏的公司清单的子集的收藏清单,并返回该清单下标。下标需要按升序排列。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    第二次做周赛的题,我目前只能想到一个思路,代码可以通过,如果有更好的思路我之后再补充。首先我写了一个helper函数帮助判断某个list是不是另一个list的subset,有了这个辅助函数之后,我再遍历input list,然后再每两个list之间比较,是否较短的那个list是较长的那个list的subset,如果是就把较短的list的index加入hashset,说明这个较短的list是别人的subset。再次遍历input的index,如果index不在hashset说明index背后的list不是任何其他list的subset,就加入结果集。

    时间O(n^3)

    空间O(n)

    Java实现

     1 class Solution {
     2     public List<Integer> peopleIndexes(List<List<String>> favoriteCompanies) {
     3         List<Integer> res = new ArrayList<>();
     4         HashSet<Integer> subsetIndex = new HashSet<>();
     5         for (int i = 0; i < favoriteCompanies.size(); i++) {
     6             for (int j = 0; j < favoriteCompanies.size(); j++) {
     7                 List<String> first = favoriteCompanies.get(i);
     8                 List<String> second = favoriteCompanies.get(j);
     9                 if (!first.equals(second)) {
    10                     if (first.size() < second.size()) {
    11                         if (isSubset(first, second)) {
    12                             subsetIndex.add(i);
    13                         }
    14                     }
    15                 }
    16             }
    17         }
    18         for (int i = 0 ; i < favoriteCompanies.size(); i++) {
    19             if (!subsetIndex.contains(i)) {
    20                 res.add(i);
    21             }
    22         }
    23         return res;
    24     }
    25     
    26     private boolean isSubset(List<String> listA, List<String> listB) {
    27         for (int i = 0; i < listA.size(); i++) {
    28             if (!listB.contains(listA.get(i))) {
    29                 return false;
    30             }
    31         }
    32         return true;
    33     }
    34 }

    LeetCode 题目总结

  • 相关阅读:
    C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本
    C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本
    C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本
    C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本
    C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本
    C# ASP.NET 优化程序性能、降低内存使用、提高程序运行速度
    C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本
    C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本
    几十套业务系统集中统一授权管理、几十万账户同步多系统实现经验分享
    C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本
  • 原文地址:https://www.cnblogs.com/cnoodle/p/12905024.html
Copyright © 2011-2022 走看看