Tạo một mảng tạm thời có kích thước (k-1) để lưu trữ các phần tử và số lượng của chúng (Phần tử đầu ra sẽ nằm trong số các phần tử k-1 này).
Di chuyển qua mảng đầu vào và cập nhật temp [] (thêm/xóa phần tử hoặc tăng/giảm số lượng) cho mọi phần tử đi ngang. Temp [] lưu trữ các ứng cử viên tiềm năng (k-1) ở mọi bước. Bước này có thời gian O (nk).
- Lặp lại qua các ứng viên tiềm năng cuối cùng (k-1) (được lưu trữ trong temp []). hoặc mọi phần tử, kiểm tra xem nó có thực sự đếm nhiều hơn n/k không. Bước này có thời gian O (nk).
Bước chính là bước 2, cách duy trì (k-1) ứng cử viên tiềm năng tại mọi thời điểm? Các bước được sử dụng trong bước 2 giống như trò chơi nổi tiếng: Tetris. Chúng tôi đối xử với mỗi con số như một mảnh trong Tetris, nằm trong temp tạm thời của chúng tôi []. Nhiệm vụ của chúng tôi là cố gắng giữ cùng một số xếp chồng lên nhau trên cùng một cột (số đếm trong mảng tạm thời được tăng lên).
Consider k = 4, n = 9
Given array: 3 1 2 2 2 1 4 3 3
i = 0
3 _ _
temp[] has one element, 3 with count 1
i = 1
3 1 _
temp[] has two elements, 3 and 1 with
counts 1 and 1 respectively
i = 2
3 1 2
temp[] has three elements, 3, 1 and 2 with
counts as 1, 1 and 1 respectively.
i = 3
- - 2
3 1 2
temp[] has three elements, 3, 1 and 2 with
counts as 1, 1 and 2 respectively.
i = 4
- - 2
- - 2
3 1 2
temp[] has three elements, 3, 1 and 2 with
counts as 1, 1 and 3 respectively.
i = 5
- - 2
- 1 2
3 1 2
temp[] has three elements, 3, 1 and 2 with
counts as 1, 2 and 3 respectively.
Now the question arises, what to do when temp[] is full and we see a new element – we remove the bottom row from stacks of elements, i.e., we decrease count of every element by 1 in temp[]. We ignore the current element.
i = 6
- - 2
- 1 2
temp[] has two elements, 1 and 2 with
counts as 1 and 2 respectively.
i = 7
- 2
3 1 2
temp[] has three elements, 3, 1 and 2 with
counts as 1, 1 and 2 respectively.
i = 8
3 - 2
3 1 2
temp[] has three elements, 3, 1 and 2 with
counts as 2, 1 and 2 respectively.
Cuối cùng, chúng tôi có tối đa số k-1 trong temp []. Các yếu tố trong tạm thời là {3, 1, 2}. Lưu ý rằng số đếm trong temp [] là vô dụng ngay bây giờ, số đếm chỉ cần ở bước 2. Bây giờ chúng ta cần kiểm tra xem số lượng thực tế của các phần tử trong temp [] có lớn hơn n/k (9/4) hay không. Các yếu tố 3 và 2 đã đếm hơn 9/4. Vì vậy, chúng tôi in 3 và 2.
Lưu ý rằng thuật toán không bỏ sót bất kỳ phần tử đầu ra nào. Có thể có hai khả năng, nhiều lần xuất hiện cùng nhau hoặc trải rộng trên mảng. Nếu các lần xuất hiện cùng nhau, thì số lượng sẽ là cao và sẽ không trở thành 0. Nếu xảy ra sự cố, thì phần tử sẽ trở lại trạng thái tạm thời []. Sau đây là C++ thực hiện các thuật toán trên.
// A C++ program to print elements with count more than n/k
#include<iostream>
using namespace std;
// A structure to store an element and its current count
struct eleCount
{
int e; // Element
int c; // Count
};
// Prints elements with more than n/k occurrences in arr[] of
// size n. If there are no such elements, then it prints nothing.
void moreThanNdK(int arr[], int n, int k)
{
// k must be greater than 1 to get some output
if (k < 2)
return;
/* Step 1: Create a temporary array (contains element
and count) of size k-1. Initialize count of all
elements as 0 */
struct eleCount temp[k-1];
for (int i=0; i<k-1; i++)
temp[i].c = 0;
/* Step 2: Process all elements of input array */
for (int i = 0; i < n; i++)
{
int j;
/* If arr[i] is already present in
the element count array, then increment its count */
for (j=0; j<k-1; j++)
{
if (temp[j].e == arr[i])
{
temp[j].c += 1;
break;
}
}
/* If arr[i] is not present in temp[] */
if (j == k-1)
{
int l;
/* If there is position available in temp[], then place
arr[i] in the first available position and set count as 1*/
for (l=0; l<k-1; l++)
{
if (temp[l].c == 0)
{
temp[l].e = arr[i];
temp[l].c = 1;
break;
}
}
/* If all the position in the temp[] are filled, then
decrease count of every element by 1 */
if (l == k-1)
for (l=0; l<k; l++)
temp[l].c -= 1;
}
}
/*Step 3: Check actual counts of potential candidates in temp[]*/
for (int i=0; i<k-1; i++)
{
// Calculate actual count of elements
int ac = 0; // actual count
for (int j=0; j<n; j++)
if (arr[j] == temp[i].e)
ac++;
// If actual count is more than n/k, then print it
if (ac > n/k)
cout << "Number:" << temp[i].e
<< " Count:" << ac << endl;
}
}
/* Driver program to test above function */
int main()
{
cout << "First Test\n";
int arr1[] = {4, 5, 6, 7, 8, 4, 4};
int size = sizeof(arr1)/sizeof(arr1[0]);
int k = 3;
moreThanNdK(arr1, size, k);
cout << "\nSecond Test\n";
int arr2[] = {4, 2, 2, 7};
size = sizeof(arr2)/sizeof(arr2[0]);
k = 3;
moreThanNdK(arr2, size, k);
cout << "\nThird Test\n";
int arr3[] = {2, 7, 2};
size = sizeof(arr3)/sizeof(arr3[0]);
k = 2;
moreThanNdK(arr3, size, k);
cout << "\nFourth Test\n";
int arr4[] = {2, 3, 3, 2};
size = sizeof(arr4)/sizeof(arr4[0]);
k = 3;
moreThanNdK(arr4, size, k);
return 0;
}
Vì vậy, bạn đã thử những gì và bạn đang gặp sự cố gì? –
Sử dụng tiếng Anh tốt ở đây. –
Bằng cách "lặp lại chính nó", bạn có nghĩa là nó phải là một lần chạy liên tiếp của cùng một số? –