给定一个由N个整数组成的数组arr [] ,任务是查找给定数组中所有频率大于⌊N/3⌋的数组元素。
例子:
Input: arr[] = {5, 3, 5}
Output: 5
Explanation:
The frequency of 5 is 2, which is more than N/3( = 3/3 = 1).
Input: arr[] = {7, 7, 7, 3, 4, 4, 4, 5}
Output: 4 7
Explanation:
The frequency of 7 and 4 in the array is 3, which is more than N/3( = 8/3 = 2).
方法:解决问题的方法是使用分而治之技术。请按照以下步骤解决问题:
- 初始化函数majorityElement(),将自左向右的任何索引返回大多数元件的计数在数组中。
- 将给定的数组arr []分成两半,然后将其重复传递给函数mostElementElement() 。
- 将低和高分别初始化为0和(N – 1) 。
- 使用以下步骤计算多数元素:
- 如果低=高:返回arr [low]作为多数元素。
- 找到中间的索引,例如mid (= (low + high)/ 2 )。
- 递归地将左右两个子数组分别作为多数元素(arr,low,mid)和多数元素(arr,mid + 1,high) 。
- 完成上述步骤后,合并两个子数组并返回多数元素。
- 每当找到所需的多数元素时,请将其附加到结果列表中。
- 打印列表中存储的所有多数元素。
下面是上述方法的实现:
Python3
# Python program for the above approach
class Solution:
# Function to find all elements that
# occurs >= N/3 times in the array
def majorityElement(self, a):
# If array is empty return
# empty list
if not a:
return []
# Function to find the majority
# element by Divide and Conquer
def divideAndConquer(lo, hi):
if lo == hi:
return [a[lo]]
# Find mid
mid = lo + (hi - lo)//2
# Call to the left half
left = divideAndConquer(lo, mid)
# Call to the right half
right = divideAndConquer(mid + 1, hi)
# Stores the result
result = []
for numbers in left:
if numbers not in right:
result.append(numbers)
result.extend(right)
# Stores all majority elements
ans = []
for number in result:
count = 0
# Count of elements that
# occurs most
for index in range(lo, hi + 1):
if a[index] == number:
count += 1
# If the number is a
# majority element
if count > (hi - lo + 1)//3:
ans.append(number)
# Return the list of element
return ans
# Function Call
print(divideAndConquer(0, len(a) - 1))
# Driver Code
if __name__ == "__main__":
# Given array a[]
a = [7, 7, 7, 3, 4, 4, 4, 6]
object = Solution()
# Function Call
object.majorityElement(a)
Python3
# Python3 program for the above approach
from collections import Counter
# Function to find the number of array
# elements with frequency more than n/3 times
def printElements(arr, n):
# Calculating n/3
x = n//3
# Counting frequency of every element using Counter
mp = Counter(arr)
# Traverse the map and print all
# the elements with occurrence atleast n/3 times
for it in mp:
if mp[it] > x:
print(it, end=" ")
# Driver code
arr = [7, 7, 7, 3, 4, 4, 4, 6]
# Size of array
n = len(arr)
# Function Call
printElements(arr, n)
# This code is contributed by vikkycirus
输出:
[7, 4]
时间复杂度: O(N * log N)
辅助空间: O(log N)
另一种方法:使用内置的Python函数:
- 使用Counter()函数计算每个元素的频率。
- 遍历频率阵列并打印所有出现在n / 3次以上的元素。
下面是实现:
Python3
# Python3 program for the above approach
from collections import Counter
# Function to find the number of array
# elements with frequency more than n/3 times
def printElements(arr, n):
# Calculating n/3
x = n//3
# Counting frequency of every element using Counter
mp = Counter(arr)
# Traverse the map and print all
# the elements with occurrence atleast n/3 times
for it in mp:
if mp[it] > x:
print(it, end=" ")
# Driver code
arr = [7, 7, 7, 3, 4, 4, 4, 6]
# Size of array
n = len(arr)
# Function Call
printElements(arr, n)
# This code is contributed by vikkycirus
输出:
7 4
时间复杂度: O(N)
辅助空间: O(N)