检查给定矩阵中的每一行是否包含从 1 到 N 的所有整数
给定一个大小为M*N的矩阵arr[][]只包含正整数,任务是检查每一行是否包含从1到N的所有整数。
例子:
Input: arr[][] = {{1, 4, 2, 3}, {2, 3, 4, 1}, {3, 4, 2, 1}}
Output: Yes
Explanation: Every row contains all the numbers from 1 to 4
Input: arr[][] = {{1, 2}, {2, 3}}
Output: No
朴素方法:最简单的方法是对矩阵的每一行进行排序,然后检查每一行是否包含从 1 到 N 的所有元素。
时间复杂度: O(M * N * logN)
辅助空间: O(M * N)
高效的方法:这个问题可以使用 Find the minimum positive number missing from an unsorted array 的方法来解决。这个想法是使用给定的矩阵作为结构来标记我们找到的数字。因此,如果找到1 到 N之间的数字,则通过反转该索引处的元素来标记与该数字对应的索引。
For example if 2 is found on row 1, change arr[1][2] to arr[1][2] = -arr[1][2]
对矩阵中的所有元素应用此操作后,矩阵应包含所有负数,否则所有行都不包含从 1 到N的数字。
下面是上述方法的实现:
C++
// C++ code for the above approach
#include
using namespace std;
// Function to check if every row contains
// all the integers from 1 to N
bool checkMat(vector >& arr)
{
int N = arr.size();
int M = arr[0].size();
for (auto& x : arr) {
for (auto& y : x) {
if (abs(y) >= 1 and abs(y) <= M) {
x[abs(y) - 1] = -x[abs(y) - 1];
}
}
}
// Check if the matrix contains
// all negatives or not
for (auto x : arr) {
for (auto y : x) {
if (y > 0) {
return 0;
}
}
}
return true;
}
// Driver Code
int main()
{
vector > arr = { { 1, 4, 2, 3 },
{ 2, 3, 4, 1 },
{ 3, 4, 2, 1 } };
if (checkMat(arr)) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to check if every row contains
// all the integers from 1 to N
static Boolean checkMat(int[][] arr)
{
int N = arr.length;
int M = arr[0].length;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (Math.abs(arr[i][j]) >= 1 && Math.abs(arr[i][j]) <= M) {
arr[i][Math.abs(arr[i][j]) - 1] = -arr[i][Math.abs(arr[i][j]) - 1];
}
}
}
// Check if the matrix contains
// all negatives or not
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (arr[i][j] > 0) {
return false;
}
}
}
return true;
}
// Driver Code
public static void main (String[] args) {
int[][] arr = { { 1, 4, 2, 3 },
{ 2, 3, 4, 1 },
{ 3, 4, 2, 1 } };
if (checkMat(arr)) {
System.out.print("Yes");
}
else {
System.out.print("No");
}
}
}
// This code is contributed by hrithikgarg03188.
Python3
# Python code for the above approach
# Function to check if every row contains
# all the integers from 1 to N
def checkMat(arr):
N = len(arr)
M = len(arr[0])
for x in arr:
for y in x:
if (abs(y) >= 1 and abs(y) <= M):
x[abs(y) - 1] = -x[abs(y) - 1]
# Check if the matrix contains
# all negatives or not
for x in arr:
for y in x:
if (y > 0):
return 0
return True
# Driver Code
arr = [[1, 4, 2, 3],
[2, 3, 4, 1],
[3, 4, 2, 1]]
if (checkMat(arr)):
print("Yes")
else:
print("No")
# This code is contributed by Saurabh Jaiswal
C#
// C# code to implement the above approach
using System;
class GFG {
// Function to check if every row contains
// all the integers from 1 to N
static Boolean checkMat(int[,] arr)
{
int N = arr.GetLength(0);
int M = arr.GetLength(1);
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (Math.Abs(arr[i, j]) >= 1 && Math.Abs(arr[i, j]) <= M) {
arr[i, Math.Abs(arr[i, j]) - 1] = -arr[i, Math.Abs(arr[i, j]) - 1];
}
}
}
// Check if the matrix contains
// all negatives or not
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (arr[i, j] > 0) {
return false;
}
}
}
return true;
}
// Driver Code
public static void Main () {
int[,] arr = { { 1, 4, 2, 3 },
{ 2, 3, 4, 1 },
{ 3, 4, 2, 1 } };
if (checkMat(arr)) {
Console.Write("Yes");
}
else {
Console.Write("No");
}
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
// JavaScript code for the above approach
// Function to check if every row contains
// all the integers from 1 to N
function checkMat(arr)
{
let N = arr.length;
let M = arr[0].length;
for (let x of arr) {
for (let y of x) {
if (Math.abs(y) >= 1 && Math.abs(y) <= M) {
x[Math.abs(y) - 1] = -x[Math.abs(y) - 1];
}
}
}
// Check if the matrix contains
// all negatives or not
for (let x of arr) {
for (let y of x) {
if (y > 0) {
return 0;
}
}
}
return true;
}
// Driver Code
let arr = [[1, 4, 2, 3],
[2, 3, 4, 1],
[3, 4, 2, 1]];
if (checkMat(arr)) {
document.write("Yes");
}
else {
document.write("No");
}
// This code is contributed by Potta Lokesh
Yes
输出
Yes
时间复杂度: O(M * N)
辅助空间: O(1)