当前位置: 代码迷 >> 综合 >> Leetcode 1452. People Whose List of Favorite Companies Is Not a Subset of Another List (python)
  详细解决方案

Leetcode 1452. People Whose List of Favorite Companies Is Not a Subset of Another List (python)

热度:50   发布时间:2023-11-26 06:20:49.0

题目

在这里插入图片描述

解法1:set

这种解法比较巧妙的运用了set和&操作,但速度挺慢

class Solution:def peopleIndexes(self, favoriteCompanies: List[List[str]]) -> List[int]:A = favoriteCompaniesdic = {
    tuple(x):ind for ind,x in enumerate(A)}A.sort(key = lambda x:len(x))ans = []for i in range(len(A)):flag = Falsefor j in range(i+1,len(A)):if set(A[i]) & set(A[j]) == set(A[i]):flag = Truebreakif not flag:ans.append(dic[tuple(A[i])])ans.sort()return ans

解法2:bitwise

未完待续
参考:
https://leetcode.com/problems/people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list/discuss/919416/Python-bitwise-with-detailed-explanation

  相关解决方案