LeetCode 1198: Find Smallest Common Element in All Rows Solution

Master LeetCode problem 1198 (Find Smallest Common Element in All Rows), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1198. Find Smallest Common Element in All Rows

Problem Explanation

Explanation:

Given a 2D integer array where each row is sorted in non-decreasing order, we need to find the smallest common element that appears in every row.

To solve this problem, we can iterate through the first row and check if each element exists in all the other rows. We can use a HashSet to keep track of the elements in the first row and then iterate through the remaining rows to find the smallest common element.

  • Initialize a HashSet to store elements from the first row.
  • Iterate through the first row and add each element to the HashSet.
  • For each element in the HashSet, iterate through the remaining rows to check if the element exists in each row.
  • If an element is found in all rows, update the smallest common element if it is smaller than the current smallest common element.

Time complexity: O(m * n), where m is the number of rows and n is the number of elements in each row. Space complexity: O(n), where n is the number of elements in the first row.

:

Solution Code

class Solution {
    public int smallestCommonElement(int[][] mat) {
        Set<Integer> firstRowElements = new HashSet<>();
        
        for (int num : mat[0]) {
            firstRowElements.add(num);
        }
        
        int smallestCommon = Integer.MAX_VALUE;
        
        for (int num : firstRowElements) {
            boolean foundInAllRows = true;
            
            for (int i = 1; i < mat.length; i++) {
                boolean foundInRow = false;
                for (int j = 0; j < mat[i].length; j++) {
                    if (mat[i][j] == num) {
                        foundInRow = true;
                        break;
                    }
                }
                
                if (!foundInRow) {
                    foundInAllRows = false;
                    break;
                }
            }
            
            if (foundInAllRows) {
                smallestCommon = Math.min(smallestCommon, num);
            }
        }
        
        return smallestCommon == Integer.MAX_VALUE ? -1 : smallestCommon;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1198 (Find Smallest Common Element in All Rows)?

This page provides optimized solutions for LeetCode problem 1198 (Find Smallest Common Element in All Rows) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1198 (Find Smallest Common Element in All Rows)?

The time complexity for LeetCode 1198 (Find Smallest Common Element in All Rows) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1198 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1198 in Java, C++, or Python.

Back to LeetCode Solutions