Advertisement

Check if Every Row and Column Contains All Numbers - LeetCode 2133 Solution

Check if Every Row and Column Contains All Numbers - Complete Solution Guide

Check if Every Row and Column Contains All Numbers is LeetCode problem 2133, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

An n x n matrix is valid if every row and every column contains all the integers from 1 to n ( inclusive ). Given an n x n integer matrix matrix , return true if the matrix is valid . Otherwise, return false . Example 1: Input: matrix = [[1,2,3],[3,1,2],[2,3,1]] Output: true Explanation: In this case, n = 3, and every row and column contains the numbers 1, 2, and 3. Hence, we return true. Example 2: Input: matrix = [[1,1,1],[1,2,3],[1,2,3]] Output: false Explanation: In this case, n = 3, but the

Detailed Explanation

The problem asks us to determine if an n x n matrix is valid. A valid matrix means that every row and every column contains all the integers from 1 to n, without any duplicates or missing numbers. The input is a square integer matrix, and the output is a boolean value: `true` if the matrix is valid, and `false` otherwise.

Solution Approach

The provided solutions utilize a set (Python) or boolean array (Java, C++, C) to efficiently check each row and column. For each row and column, it iterates through the elements, adding them to the set/marking them in the boolean array. If a duplicate is encountered, or if the number is outside the valid range (1 to n), the function immediately returns `false`. After processing all elements of a row/column, it verifies if the set/array contains all numbers from 1 to n. If not, it returns `false`. If all rows and columns pass these checks, the function returns `true`.

Step-by-Step Algorithm

  1. Step 1: Iterate through each row of the matrix.
  2. Step 2: For each row, create a set (or boolean array) to store the numbers encountered in that row. Iterate through the elements of the row, adding each element to the set (or marking it in the array). If a duplicate is found or a number is out of range, immediately return `false`.
  3. Step 3: After processing all elements of the row, check if the set (or array) contains all numbers from 1 to n. If not, immediately return `false`.
  4. Step 4: Repeat steps 1-3 for each column of the matrix. This involves iterating through columns and using another set (or boolean array) for each column.
  5. Step 5: If all rows and columns pass the checks, return `true`.

Key Insights

  • Insight 1: We need to check both rows and columns independently for completeness. A simple nested loop isn't sufficient to efficiently handle both checks simultaneously.
  • Insight 2: Using a set (or boolean array) to track the presence of numbers in each row and column is efficient. Sets provide fast membership checking (O(1) on average). Boolean arrays offer a slightly more space-efficient option in this specific case.
  • Insight 3: We can optimize slightly by returning `false` immediately if we find a number outside the range [1, n] or a duplicate within a row or column. This avoids unnecessary further checks.

Complexity Analysis

Time Complexity: O(n^2)

Space Complexity: O(n)

Topics

This problem involves: Array, Hash Table, Matrix.

Companies

Asked at: Indeed, Instacart, Karat.