给定一个大小为N×M的矩阵mat[][] ,其中矩阵的每一行都是来自[1, M]的元素的排列,任务是找到矩阵每一行中存在的子数组的最大长度.
例子:
Input: mat[][] = {{1, 2, 3, 4, 5}, {2, 3, 4, 1, 5}, {5, 2, 3, 4, 1}, {1, 5, 2, 3, 4}}
Output: 3
Explanation:
In each row, {2, 3, 4} is the longest common sub-array present in all the rows of the matrix.
Input: mat[][] = {{4, 5, 1, 2, 3, 6, 7}, {1, 2, 4, 5, 7, 6, 3}, {2, 7, 3, 4, 5, 1, 6}}
Output: 2
朴素方法:解决问题的最简单方法是生成矩阵第一行的所有可能子数组,然后检查剩余行是否包含该子数组。
时间复杂度: O(M×N 2 )
辅助空间: O(N)
高效的方法:上述方法可以通过创建一个矩阵来优化,比如dp[][]存储每一行中元素的位置,然后检查每行中当前和前一个元素的索引是否相差1 .请按照以下步骤解决问题:
- 初始化一个矩阵,比如dp[][] ,它存储每一行中每个元素的位置。
- 要填充矩阵dp[][] ,请使用变量i在范围[0, N-1] 中迭代并执行以下步骤:
- 使用变量j在范围[0, M-1] 中迭代并将dp[i][arr[i][j]]的值修改为j 。
- 初始化变量,假设ans存储所有行中公共的最长子数组的长度, len存储所有行中公共的子数组的长度。
- 使用变量i在范围[1, M-1] 中迭代并执行以下步骤:
- 将布尔变量check初始化为1 ,检查a[i][0]是否在每行中a[i-1][0]之后。
- 使用变量j在范围[1, N-1] 中迭代并执行以下步骤:
- 如果dp[j][arr[0][i-1]] + 1 != dp[j][arr[0][i]] ,将 check 的值修改为0并终止循环。
- 如果检查的值为1 ,则将len的值增加1并将ans的值修改为max(ans, len) 。
- 如果检查的值为0 ,则将len的值修改为1 。
- 完成以上步骤后,打印ans的值作为答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find longest common subarray
// in all the rows of the matrix
int largestCommonSubarray(
vector > arr,
int n, int m)
{
// Array to store the position
// of element in every row
int dp[n][m + 1];
// Traverse the matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
// Store the position of
// every element in every row
dp[i][arr[i][j]] = j;
}
}
// Variable to store length of largest
// common Subarray
int ans = 1;
int len = 1;
// Traverse through the matrix column
for (int i = 1; i < m; i++) {
// Variable to check if every row has
// arr[i][j] next to arr[i-1][j] or not
bool check = true;
// Traverse through the matrix rows
for (int j = 1; j < n; j++) {
// Check if arr[i][j] is next to
// arr[i][j-1] in every row or not
if (dp[j][arr[0][i - 1]] + 1
!= dp[j][arr[0][i]]) {
check = false;
break;
}
}
// If every row has arr[0][j] next
// to arr[0][j-1] increment len by 1
// and update the value of ans
if (check) {
len++;
ans = max(ans, len);
}
else {
len = 1;
}
}
return ans;
}
// Driver Code
int main()
{
// Given Input
int n = 4;
int m = 5;
vector > arr{ { 4, 5, 1, 2, 3, 6, 7 },
{ 1, 2, 4, 5, 7, 6, 3 },
{ 2, 7, 3, 4, 5, 1, 6 } };
int N = arr.size();
int M = arr[0].size();
// Function Call
cout << largestCommonSubarray(arr, N, M);
return 0;
}
Java
// Java program for the above approach
import java.lang.*;
import java.util.*;
class GFG{
// Function to find longest common subarray
// in all the rows of the matrix
static int largestCommonSubarray(int[][] arr,
int n, int m)
{
// Array to store the position
// of element in every row
int dp[][] = new int[n][m + 1];
// Traverse the matrix
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
// Store the position of
// every element in every row
dp[i][arr[i][j]] = j;
}
}
// Variable to store length of largest
// common Subarray
int ans = 1;
int len = 1;
// Traverse through the matrix column
for(int i = 1; i < m; i++)
{
// Variable to check if every row has
// arr[i][j] next to arr[i-1][j] or not
boolean check = true;
// Traverse through the matrix rows
for(int j = 1; j < n; j++)
{
// Check if arr[i][j] is next to
// arr[i][j-1] in every row or not
if (dp[j][arr[0][i - 1]] + 1 !=
dp[j][arr[0][i]])
{
check = false;
break;
}
}
// If every row has arr[0][j] next
// to arr[0][j-1] increment len by 1
// and update the value of ans
if (check)
{
len++;
ans = Math.max(ans, len);
}
else
{
len = 1;
}
}
return ans;
}
// Driver code
public static void main(String[] args)
{
// Given Input
int n = 4;
int m = 5;
int[][] arr = { { 4, 5, 1, 2, 3, 6, 7 },
{ 1, 2, 4, 5, 7, 6, 3 },
{ 2, 7, 3, 4, 5, 1, 6 } };
int N = arr.length;
int M = arr[0].length;
// Function Call
System.out.println(largestCommonSubarray(arr, N, M));
}
}
// This code is contributed by avijitmondal1998
Python3
# Python3 program for the above approach
# Function to find longest common subarray
# in all the rows of the matrix
def largestCommonSubarray(arr, n, m):
# Array to store the position
# of element in every row
dp = [[0 for i in range(m+1)] for j in range(n)]
# Traverse the matrix
for i in range(n):
for j in range(m):
# Store the position of
# every element in every row
dp[i][arr[i][j]] = j
# Variable to store length of largest
# common Subarray
ans = 1
len1 = 1
# Traverse through the matrix column
for i in range(1,m,1):
# Variable to check if every row has
# arr[i][j] next to arr[i-1][j] or not
check = True
# Traverse through the matrix rows
for j in range(1,n,1):
# Check if arr[i][j] is next to
# arr[i][j-1] in every row or not
if (dp[j][arr[0][i - 1]] + 1 != dp[j][arr[0][i]]):
check = False
break
# If every row has arr[0][j] next
# to arr[0][j-1] increment len by 1
# and update the value of ans
if (check):
len1 += 1
ans = max(ans, len1)
else:
len1 = 1
return ans
# Driver Code
if __name__ == '__main__':
# Given Input
n = 4
m = 5
arr = [[4, 5, 1, 2, 3, 6, 7],
[1, 2, 4, 5, 7, 6, 3],
[2, 7, 3, 4, 5, 1, 6]]
N = len(arr)
M = len(arr[0])
# Function Call
print(largestCommonSubarray(arr, N, M))
# This code is contributed by bgangwar59.
Javascript
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find longest common subarray
// in all the rows of the matrix
static int largestCommonSubarray(int [,]arr,
int n, int m)
{
// Array to store the position
// of element in every row
int [,]dp = new int[n,m + 1];
// Traverse the matrix
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
// Store the position of
// every element in every row
dp[i,arr[i,j]] = j;
}
}
// Variable to store length of largest
// common Subarray
int ans = 1;
int len = 1;
// Traverse through the matrix column
for(int i = 1; i < m; i++)
{
// Variable to check if every row has
// arr[i][j] next to arr[i-1][j] or not
bool check = true;
// Traverse through the matrix rows
for(int j = 1; j < n; j++)
{
// Check if arr[i][j] is next to
// arr[i][j-1] in every row or not
if (dp[j,arr[0,i - 1]] + 1 !=
dp[j,arr[0,i]])
{
check = false;
break;
}
}
// If every row has arr[0][j] next
// to arr[0][j-1] increment len by 1
// and update the value of ans
if (check == true)
{
len++;
ans = Math.Max(ans, len);
}
else
{
len = 1;
}
}
return ans;
}
// Driver code
public static void Main()
{
// Given Input
int [,]arr = { { 4, 5, 1, 2, 3, 6, 7 },
{ 1, 2, 4, 5, 7, 6, 3 },
{ 2, 7, 3, 4, 5, 1, 6 } };
int N = 3;
int M = 7;
// Function Call
Console.Write(largestCommonSubarray(arr, N, M));
}
}
// This code is contributed by _saurabh_jaiswal.
输出
2
时间复杂度: O(N×M)
辅助空间: O(N×M)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。