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.

    解题思路:首先把所有的公司排序,接下来利用公司的下标为每一个收藏列表算出一个特征值。例如某人收藏了编号为1,2,4的公司,那么这个收藏列表的特征值就是:2^1+2^2+2^4。算出所有收藏列表的特征值后,判断一个收藏是否为另一个收藏的子集,只需要将两个特征值做"或"操作,如果结果和其中一个特征值相等,那么其中一个即为另一个的子集。

    代码如下:

    class Solution(object):
        def peopleIndexes(self, favoriteCompanies):
            """
            :type favoriteCompanies: List[List[str]]
            :rtype: List[int]
            """
            dic_companies = {}
            inx = 0
            for companies in favoriteCompanies:
                for company in companies:
                    if company not in dic_companies:
                        dic_companies[company] = inx
                        inx += 1
    
            favorite_comp = []
            for companies in favoriteCompanies:
                val = 0
                for company in companies:
                    val += 2** dic_companies[company]
                favorite_comp.append(val)
    
            res = []
    
            for i in range(len(favorite_comp)):
                flag = True
                for j in range(len(favorite_comp)):
                    if i == j:continue
                    if favorite_comp[i] | favorite_comp[j] == favorite_comp[j]:
                        flag = False
                        break
                if flag:
                    res.append(i)
    
            return res
  • 相关阅读:
    msql 计算连续签到天数
    jetty启动常用命令
    nginx负载均衡, 配置地址带端口
    IDEA java 代码格式化统一
    Linux下安装Zookeeper
    nexus admin 从文件角度进行密码重置
    Monkey测试
    接口测试
    我的IT之路
    cookie 操作(转载)
  • 原文地址:https://www.cnblogs.com/seyjs/p/13046848.html
Copyright © 2011-2022 走看看