LeetCode题解(1198):找出所有行中最小公共元素(Python)
发布日期:2021-06-29 20:09:49 浏览次数:2 分类:技术文章

本文共 868 字,大约阅读时间需要 2 分钟。

题目:(中等)

标签:哈希表、二分查找

解法 时间复杂度 空间复杂度 执行用时
Ans 1 (Python) O ( N l o g M ) O(NlogM) O(NlogM) O ( N ) O(N) O(N) 80ms (78.41%)
Ans 2 (Python)
Ans 3 (Python)

解法一:

class Solution:    def smallestCommonElement(self, mat: List[List[int]]) -> int:        size1, size2 = len(mat), len(mat[0])        hashmap = {
i: 0 for i in range(size1)} now = [m[0] for m in mat] while len(set(now)) > 1: max_val = max(now) for i in range(size1): idx = bisect.bisect(mat[i], max_val, lo=hashmap[i]) # print(mat[i], max_val, "->", idx - 1) if mat[i][idx - 1] == max_val: hashmap[i] = idx - 1 elif idx >= size2: return -1 else: hashmap[i] = idx now = [mat[i][hashmap[i]] for i in range(size1)] return now[0]

转载地址:https://dataartist.blog.csdn.net/article/details/109848458 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:LeetCode题解(1213):三个有序数组的交集(Python)
下一篇:LeetCode题解(1178):猜字谜(Python)

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年05月01日 07时53分06秒