用于数组元素频率范围查询的 Python3 程序
给定一个包含 n 个非负整数的数组。任务是在 array[] 的任意范围内找到特定元素的频率。范围作为数组中的位置(不是基于 0 的索引)给出。可以有多个给定类型的查询。
例子:
Input : arr[] = {2, 8, 6, 9, 8, 6, 8, 2, 11};
left = 2, right = 8, element = 8
left = 2, right = 5, element = 6
Output : 3
1
The element 8 appears 3 times in arr[left-1..right-1]
The element 6 appears 1 time in arr[left-1..right-1]
天真的方法:是从左到右遍历并在找到元素时更新计数变量。
以下是 Naive 方法的代码:-
Python3
# Python program to find total
# count of an element in a range
# Returns count of element
# in arr[left-1..right-1]
def findFrequency(arr, n, left, right, element):
count = 0
for i in range(left - 1, right):
if (arr[i] == element):
count += 1
return count
# Driver Code
arr = [2, 8, 6, 9, 8, 6, 8, 2, 11]
n = len(arr)
# Print frequency of 2 from position 1 to 6
print("Frequency of 2 from 1 to 6 = ",
findFrequency(arr, n, 1, 6, 2))
# Print frequency of 8 from position 4 to 9
print("Frequency of 8 from 4 to 9 = ",
findFrequency(arr, n, 4, 9, 8))
# This code is contributed by Anant Agarwal.
Python3
# Python3 program to find total count of an element
from collections import defaultdict as dict
from bisect import bisect_left as lower_bound
from bisect import bisect_right as upper_bound
store = dict(list)
# Returns frequency of element
# in arr[left-1..right-1]
def findFrequency(arr, n, left, right, element):
# Find the position of
# first occurrence of element
a = lower_bound(store[element], left)
# Find the position of
# last occurrence of element
b = upper_bound(store[element], right)
return b - a
# Driver code
arr = [2, 8, 6, 9, 8, 6, 8, 2, 11]
n = len(arr)
# Storing the indexes of
# an element in the map
for i in range(n):
store[arr[i]].append(i + 1)
# Print frequency of 2 from position 1 to 6
print("Frequency of 2 from 1 to 6 = ",
findFrequency(arr, n, 1, 6, 2))
# Print frequency of 8 from position 4 to 9
print("Frequency of 8 from 4 to 9 = ",
findFrequency(arr, n, 4, 9, 8))
# This code is contributed by Mohit Kumar
输出:
Frequency of 2 from 1 to 6 = 1
Frequency of 8 from 4 to 9 = 2
这种方法的时间复杂度是 O(right – left + 1) 或 O(n)
辅助空间:O(1)
一种有效的方法是使用散列。在 C++ 中,我们可以使用 unordered_map
- 首先,我们将每个不同元素的位置作为向量存储在 map[] 中
int arr[] = {2, 8, 6, 9, 8, 6, 8, 2, 11};
map[2] = {1, 8}
map[8] = {2, 5, 7}
map[6] = {3, 6}
ans so on...
- 我们可以看到 map[] 中的元素已经按排序顺序排列(因为我们从左到右插入元素),答案归结为使用类似二进制搜索的方法找到该哈希 map[] 中的总数。
- 在 C++ 中,我们可以使用 lower_bound 它将返回一个迭代器,该迭代器指向范围 [first, last] 中的第一个元素,其值不小于“left”。并且upper_bound 返回一个迭代器,该迭代器指向[first,last) 范围内的第一个元素,其值大于'right'。
- 之后我们只需要减去upper_bound() 和lower_bound() 的结果就可以得到最终的答案。例如,假设我们要在 [1 到 6] 的范围内找到 8 的总数,那么 lower_bound()函数的 map[8] 将返回结果 0(指向 2),upper_bound() 将返回 2(指向 7),所以我们需要减去两个结果,如 2 – 0 = 2 。
以下是上述方法的代码
Python3
# Python3 program to find total count of an element
from collections import defaultdict as dict
from bisect import bisect_left as lower_bound
from bisect import bisect_right as upper_bound
store = dict(list)
# Returns frequency of element
# in arr[left-1..right-1]
def findFrequency(arr, n, left, right, element):
# Find the position of
# first occurrence of element
a = lower_bound(store[element], left)
# Find the position of
# last occurrence of element
b = upper_bound(store[element], right)
return b - a
# Driver code
arr = [2, 8, 6, 9, 8, 6, 8, 2, 11]
n = len(arr)
# Storing the indexes of
# an element in the map
for i in range(n):
store[arr[i]].append(i + 1)
# Print frequency of 2 from position 1 to 6
print("Frequency of 2 from 1 to 6 = ",
findFrequency(arr, n, 1, 6, 2))
# Print frequency of 8 from position 4 to 9
print("Frequency of 8 from 4 to 9 = ",
findFrequency(arr, n, 4, 9, 8))
# This code is contributed by Mohit Kumar
输出:
Frequency of 2 from 1 to 6 = 1
Frequency of 8 from 4 to 9 = 2
如果我们有大量任意范围的查询询问特定元素的总频率,这种方法将是有益的。
时间复杂度:单个查询的 O(log N)。
有关更多详细信息,请参阅有关数组元素频率的范围查询的完整文章!