Leetcode # 1198. Find Smallest Common Element in All Rows
Problem
https://leetcode.com/problems/find-smallest-common-element-in-all-rows
Solution: Binary Search
Time Complexity: O(len(mat) * log(len(mat[0])))
Space Complexity: O(1)
(The input and output generally do not count towards the space complexity.)
class Solution:
def smallestCommonElement(self, mat: List[List[int]]) -> int:
for n in mat[0]:
for i in range(1, len(mat)):
row = mat[i]
if (_i := bisect.bisect(row, n)) == 0 or row[_i - 1] != n:
break
if i == len(mat) - 1:
return n
return -1
Last Updated on 2025/08/09 by A1go