计算具有相似数大于K的子数组数量

3

Similarity number for two arrays X and Y, each with size N, is defined as the number of pairs of indices (i,j) such that X[i]=Y[j] , for 1<=i,j

Now we are given two arrays, of size N and M. We need to find the number of sub arrays of equal sizes from these two arrays such that the similairty number of each subarray pair is greater or equal to given number K.

Example, say we have N=3, M=3, K=1 and arrays be [1,3,4] and [1,5,3] then here answer is 6

Explanation :

({1},{1})
({3},{3})
({1,3},{1,5})
({1,3},{5,3})
({3,4},{5,3})
({1,3,4},{1,5,3})

so ans = 6

How to solve it for given arrays of size N,M and given integer K. Number of elements can't be more than 2000. K is also less than N*M

Approach : Form all subarrays from array 1 of size N, those will be N*(N+1)/2 And same for array 2 of size M. Then try to find similarity number between each subarray pair. But this is very unoptimised way of doing it. What can be better way to solve this problem ? I think Dynamic programming can be used to solve this. Any suggestions ?

For {1,1,2} and {1,1,3} and K=1

{[1(1)],[1(1)]}
{[1(1)],[1(2)]}
{[1(2)],[1(1)]}
{[1(2)],[1(2)]}
{[1(1),1(2)],[1(1)]}
{[1(1),1(2)],[1(2)]}
{[1(1)],[1(1),1(2)]}
{[1(2)],[1(1),1(2)]}
{[1(1),1(2)],[1(1),1(2)]}
{[1(2),2],[1(2),3]}
{[1(1),1(2),2],[1(1),1(2),3]}


重复值是否允许?如果是这样,那么{1,1,2}和{1,1,3}的相似度是2还是4?当K=1时,({1},{1})的出现次数是计算一次、两次还是四次? - trincot
@trincot 这是针对每一对 I 和 j 的。因此,如果我们选择 array1 的第一个元素,array2 的第二个元素,这与 array1 的第二个元素和 array2 的第一个元素不同。因此,当 K=1 时,({1},{1}) 的出现次数为四。 - AlgoLover
@trincot 如果一个子数组的起始或结束索引或两者都不同于所选数组,则该子数组是唯一的。 - AlgoLover
最好将示例放在问题本身中。在评论部分阅读起来很困难。 - trincot
1
@trincot 已完成。 :) - AlgoLover
这是关于HackerEarth生动竞赛的问题。https://www.hackerearth.com/challenge/competitive/december-circuits-17/algorithm/two-arrays-2-0f24abf0/ - Vikram Kumar
1个回答

0

既然比赛已经结束,为了完整起见,这是我对编辑答案的理解(我从中学到了很多)。假设我们有一个O(1)时间方法来计算两个连续子数组的相似度,每个数组中都有一个长度为l。然后,对于每一对索引(i, j),我们可以二分查找最小的l(例如向左扩展),使得相似度为k。(一旦我们有了最小的l,我们就知道任何更大的l也具有足够的相似度,并且我们可以在O(1)时间内添加这些计数。)在这种情况下,总时间将为O(M * N * log(max (M,N))

原来,有一种方法可以在O(1)时间内计算两个连续子数组的相似度:矩阵前缀和。在一个矩阵A中,每个条目A(i,j)如果第一个数组的第i个元素等于第二个数组的第j个元素,则为1,否则为0;在矩形A(i-l, j-l),A(i,j)(从左上到右下)中A的元素之和正是它们的相似度。通过矩阵前缀和,在预处理O(M*N)的情况下,我们可以在O(1)时间内计算出该总和。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接