本文主要是介绍leetcode 第189周周赛 收藏清单,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
解题思路:用哈希表存储某个公司出现过的清单,对每一个清单求每个每个公司清单的并集,如果并集不为空说明这个清单被包含在其他清单中
代码如下:
class Solution:def peopleIndexes(self, favoriteCompanies: List[List[str]]) -> List[int]:tabel = {}size = len(favoriteCompanies)length = []for i in range(size):length.append(len(favoriteCompanies[i]))for x in favoriteCompanies[i]:if x not in tabel:tabel[x] = [i]else:tabel[x].append(i)import numpy as npindex = list(np.argsort(np.array(length)))res = []for i in range(size):if i == size -1:tmp = []j = i - 1while(j >= 0 and length[index[j]] == length[index[i]]):tmp.append(index[j])j -= 1tmp = set(tmp)else:tmp = set(index[i+1:])for x in favoriteCompanies[index[i]]:tmp = tmp & set(tabel[x])if len(tmp) == 0:res.append(int(index[i]))res = sorted(res)return res
这篇关于leetcode 第189周周赛 收藏清单的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!