Check if Every Row and Column Contains All Numbers
To check if a given matrix is valid, we need to ensure that every row and every column contains all the integers from 1 to n (inclusive).
One approach to solve this problem is to iterate through each row and column of the matrix, and check if they contain all integers from 1 to n.
Python solution:
|
|
In this solution, we first create a set of all numbers from 1 to n. Then we iterate over each row of the matrix and create a set from the elements in that row. If this set is not equal to the set of all numbers from 1 to n, we return False, indicating the matrix is not valid. We then do the same for each column of the matrix. If none of the rows or columns invalidate the matrix, we return True.
This solution has a time complexity of O(n^2) as we are iterating over each element of the matrix twice, once for the rows and once for the columns.