Consider an array A[] which can contain non distinct integers, how can I find the shortest range which covers all the elements present in the array?
考虑一个可以包含非不同整数的数组A [],如何找到覆盖数组中所有元素的最短范围?
A[] = 7 3 1 7 3 1 3 4 1
index= 0 1 2 3 4 5 6 7 8
Elements in the range [3,7] form the shortest range present in the array.
Hence the answer must be 5.
How can I solve this problem using double pointer?
P.S.:This question is not from any live contests.
My try: I had taken a variable len
which i would vary from 1 to length of array and for each len
check whether it covers all the elements or not.
范围[3,7]中的元素形成阵列中存在的最短范围。因此答案必须是5.如何使用双指针解决此问题? P.S .:这个问题不是来自任何现场比赛。我的尝试:我已经采用了一个变量len,我将从1到数组的长度变化,并且每个len检查它是否覆盖所有元素。
1 个解决方案
#1
2
Let's array length be N
and the number of distinctive elements be K
, K<=N
. Let's assume the values are 0
, 1
, ..., K-1
. (We can sort all elements, then replace elements with minimum value with 0
, elements with second minimum value with 1
, etc., then put them back to the original positions. It requires O(N*log(N)
complexity.)
设数的长度为N,区别元素的数量为K,K <= N。假设值为0,1,...,K-1。 (我们可以对所有元素进行排序,然后用0表示最小值的元素,用1表示第二最小值的元素等,然后将它们放回原始位置。它需要O(N * log(N)复杂度。)
Make an array C[K]
, where C[i]
should keep count of elements with value i
in a segment [x, y]
. Let D
be a number of distinct elements in a segment.
创建一个数组C [K],其中C [i]应该在一个段[x,y]中保持值为i的元素的计数。设D是段中的多个不同元素。
If we extend such segment by increasing y
, we increase C[A[y+1]]
by one. If C[A[y+1]]
becomes 1
, we increase D
by 1
.
如果我们通过增加y来扩展这样的段,我们将C [A [y + 1]]增加1。如果C [A [y + 1]]变为1,我们将D增加1。
If we shrink the segment by increasing x
, we decrease C[A[x+1]]
by one. If C[A[x+1]]
becomes 0
, we decrease D
by 1
.
如果我们通过增加x来缩小线段,我们将C [A [x + 1]]减1。如果C [A [x + 1]]变为0,我们将D减1。
So, if D==K
we know that the segment contains all elements.
因此,如果D == K,我们知道该段包含所有元素。
Now start from [0, 0]
and keep extending the segment till it contains all possible values. Ley it be [0, q]
. Then shrink it while the resulting segment still has elements with all possible values. Let it be [p, q]
.
现在从[0,0]开始并继续扩展段直到它包含所有可能的值。嘿,它是[0,q]。然后缩小它,同时结果段仍然包含具有所有可能值的元素。让它成为[p,q]。
Move that [p, q]
right by one position, i.e. to [p+1, q+1]
. Again, try to shrink the segment. Etc.
将[p,q]向右移动一个位置,即到[p + 1,q + 1]。再次尝试缩小细分。等等。
So, roughly speaking, move the segment from left to right, and try shrinking it on every step. When you reach the end and cannot move the left end any more, you are done. It is O(N)
. So all together (including sorting): O(N*log(N))
.
所以,粗略地说,将片段从左向右移动,并尝试在每一步上缩小它。当你到达终点并且不能再移动左端时,你就完成了。它开着)。所以一起(包括排序):O(N * log(N))。
#1
2
Let's array length be N
and the number of distinctive elements be K
, K<=N
. Let's assume the values are 0
, 1
, ..., K-1
. (We can sort all elements, then replace elements with minimum value with 0
, elements with second minimum value with 1
, etc., then put them back to the original positions. It requires O(N*log(N)
complexity.)
设数的长度为N,区别元素的数量为K,K <= N。假设值为0,1,...,K-1。 (我们可以对所有元素进行排序,然后用0表示最小值的元素,用1表示第二最小值的元素等,然后将它们放回原始位置。它需要O(N * log(N)复杂度。)
Make an array C[K]
, where C[i]
should keep count of elements with value i
in a segment [x, y]
. Let D
be a number of distinct elements in a segment.
创建一个数组C [K],其中C [i]应该在一个段[x,y]中保持值为i的元素的计数。设D是段中的多个不同元素。
If we extend such segment by increasing y
, we increase C[A[y+1]]
by one. If C[A[y+1]]
becomes 1
, we increase D
by 1
.
如果我们通过增加y来扩展这样的段,我们将C [A [y + 1]]增加1。如果C [A [y + 1]]变为1,我们将D增加1。
If we shrink the segment by increasing x
, we decrease C[A[x+1]]
by one. If C[A[x+1]]
becomes 0
, we decrease D
by 1
.
如果我们通过增加x来缩小线段,我们将C [A [x + 1]]减1。如果C [A [x + 1]]变为0,我们将D减1。
So, if D==K
we know that the segment contains all elements.
因此,如果D == K,我们知道该段包含所有元素。
Now start from [0, 0]
and keep extending the segment till it contains all possible values. Ley it be [0, q]
. Then shrink it while the resulting segment still has elements with all possible values. Let it be [p, q]
.
现在从[0,0]开始并继续扩展段直到它包含所有可能的值。嘿,它是[0,q]。然后缩小它,同时结果段仍然包含具有所有可能值的元素。让它成为[p,q]。
Move that [p, q]
right by one position, i.e. to [p+1, q+1]
. Again, try to shrink the segment. Etc.
将[p,q]向右移动一个位置,即到[p + 1,q + 1]。再次尝试缩小细分。等等。
So, roughly speaking, move the segment from left to right, and try shrinking it on every step. When you reach the end and cannot move the left end any more, you are done. It is O(N)
. So all together (including sorting): O(N*log(N))
.
所以,粗略地说,将片段从左向右移动,并尝试在每一步上缩小它。当你到达终点并且不能再移动左端时,你就完成了。它开着)。所以一起(包括排序):O(N * log(N))。