如何解决了解二进制搜索背后的直觉,以查找较小最左边的occ和较大最右边的occ元素具有重复项的计数
参考:https://en.wikipedia.org/wiki/Binary_search_algorithm
function binary_search_leftmost(A,n,T):
L := 0
R := n
while L < R:
m := floor((L + R) / 2)
if A[m] < T:
L := m + 1
else:
R := m
return L
Wikipedia提供了此功能来查找元素的最左侧出现,这神奇地(至少对我来说:P)表示小于给定目标的元素数目(无论目标是否在数组中)
例如,考虑大小为10的数组[1,2,4,7,9,16]
。
wiki_leftmost(lst,0) == 0
0 is not in the array.
The answer 0 represents the elements < 0 which is 0.
wiki_leftmost(lst,1) == 0
1 is in the array. The answer 0 represent in the index of the leftmost occurence of 1.
The answer also represents the elements < 1 which is 1.
wiki_leftmost(lst,4) == 2
4 is in the array. The answer 2 represent in the index of the leftmost occurence of 4.
The answer also represents the elements < 4 which is 2.
wiki_leftmost(lst,7) == 7
7 is in the array. The answer 7 represent in the index of the leftmost occurence of 4.
The answer also represents the elements < 7 which is 7.
wiki_leftmost(lst,10) == 9
10 is not in the array.
The answer 9 represents the elements < 10 which is 9.
wiki_leftmost(lst,16) == 9
16 is in the array. The answer 9 represent in the index of the leftmost occurence of 16.
The answer also represents the elements < 16 which is 9.
wiki_leftmost(lst,200) == 10
20 is not in the array.
The answer 10 represents the elements < 200 which is 10.
我理解了普通二进制搜索(非常简单)背后的直觉,以及它的替代版本(没有相等条件的版本)(两者都存在于Wiki页面中)。但是我不太了解该算法背后的直觉。我以示例为例,并确实完成了流程,但是我需要适当的直觉才能可视化和理解更多算法。
PS:也有一些类似的问题,但是所有这些细节都没有提到,而且我也不满意。
解决方法
目标:找到最左侧,使得A[L]
== T
和L
最小。
我们重塑了检查相等性的目标,因为原始的二进制搜索可能在多个重复对象的中间给出某个元素,相反,我们说如果可以找到最后一个元素的索引小于T,则答案可能是下一个元素是否存在(如果存在)。请参阅伪代码中的注释。
function binary_search_leftmost(A,n,T):
L := 0
R := n
while L < R:
m := floor((L + R) / 2) # partition in 2 parts get middle
if A[m] < T: # last element seen so far which is less than T
L := m + 1 # this could possibly be A[L] == T,if not keep searching towards right
else:
R := m # A[m] >= T,mth element could be our answer
return L # after this we still need to check if L < n and A[L] == T
类似地,对于最右边的匹配,我们进行重塑以查找大于T的最右边的元素,我们的目标元素可能恰好在最后一个最右边的元素之前。请参阅伪代码中的注释。
function binary_search_rightmost(A,T):
L := 0
R := n
while L < R:
m := floor((L + R) / 2) # partition in 2 parts get middle
if A[m] > T: # last element seen so far which is greater than T
R := m # our answer lies between [L,m],since we are interested in A[m] > T and it is possible A[m] is the only greater element than T
else:
L := m + 1 # since A[m] <= T and we are only interested in A[i] > T
return R - 1 # return previous element,still need to check if R-1 >= 0 and A[R-1] == T
,
在function binary_search_leftmost(A,T):
中
如果n
是数组A
中的元素数,而T
是要搜索的元素,
并且在您分别分配L
和R
,0
和n
时。
R
的值将不在索引中,因为随着索引从0
开始,索引应以比元素总数1
少的(n)
结束function binary_search_leftmost(A,T):
L := 0
R := n - 1
while L < R:
m := floor((L + R) / 2)
if A[m] < T:
L := m + 1
else:
R := m - 1
return L
>
因此,函数应如下所示:
pandas
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。