Published: Sep 27, 2022
Introduction
The problem is about n x n matrix and 1 to n (inclusive) values. Using a set data structure, check cells of each row and columns doesn’t duplicate.
Problem Description
An
n x nmatrix is valid if every row and every column contains all the integers from 1 to n (inclusive).Given an
n x ninteger matrixmatrix, returntrueif the matrix is valid. Otherwise, returnfalse.Constraints:
n == matrix.length == matrix[i].length1 <= n <= 1001 <= matrix[i][j] <= nhttps://leetcode.com/problems/check-if-every-row-and-column-contains-all-numbers/
Examples
Example 1
Input: matrix = [[1,2,3],[3,1,2],[2,3,1]]
Output: true
Example 2
Input: matrix = [[1,1,1],[1,2,3],[1,2,3]]
Output: false
Analysis
The solution uses a set for horizontal and array of sets for vertical. When a row is scanned, it checks the horizontal set. When all rows are scanned, it checks the array of vertical sets. If all are length n, return True.
Solution
class CheckAllNumbers:
def checkValid(self, matrix: List[List[int]]) -> bool:
n = len(matrix)
vertical = [set() for _ in range(n)]
for i in range(n):
horizontal = set()
for j in range(n):
cur = matrix[i][j]
horizontal.add(cur)
vertical[j].add(cur)
if len(horizontal) != n:
return False
for i in range(n):
if len(vertical[i]) != n:
return False
return True
Complexities
- Time:
O(n^2) - Space:
O(n^2)