C++程序:统计数组中大于其左侧所有元素且大于其右侧至少K个元素的元素个数
字符串是一个对象,它表示一系列数据字符。字符串是数据容器,始终以文本格式表示。它也用于概念、比较、分割、连接、替换、修剪、长度、内部化、相等、比较、子字符串操作。使用快速排序分区算法查找数组中最大的K个(或最小的K个)元素。
这里有一个包含N个不同整数的数组R[]。任务是找到那些严格大于其前面所有元素且严格大于其右侧至少K个元素的特定元素。问题陈述如下:给定一个包含N个不同元素的数组arr[ ](数组R)和整数K;我们必须找出有多少个元素大于其左侧所有元素,且大于其右侧至少K个元素。
Input: R[] = {16,07,10,22,2001,1997}, K = 3
Output: 16
Therefore, the count is 2.
有两种方法可以找出数组中大于其左侧所有元素且大于其右侧至少K个元素的元素。
朴素方法 − 这是遍历特定数组的最简单方法。在这种方法中,我们必须遍历左侧的所有元素以检查它是否小于当前元素。否则,遍历右侧以检查至少K个元素是否小于当前元素。对于这种方法,时间复杂度为O(N2),辅助空间为O(1)。
高效方法 − 这是一种可以通过自平衡BST进行优化的过程。使用AVL树从右到左遍历数组。AVL树生成一个数组countSmaller[]。这里的时间复杂度为O(NlogN),辅助空间为O(N)。对于每个满足条件的元素,计数器加1。
让我们找出数组中大于其左侧所有元素且大于其右侧至少K个元素的元素个数。
统计大于所有元素的数组元素的算法:
在这个算法中,我们将遵循一步一步的过程来计算数组元素。通过这个,我们将构建一些C++代码来查找最大的元素。
步骤1 − 开始。
步骤2 − 从右到左遍历数组。
步骤3 − 将所有元素插入AVL树。
步骤4 − 使用AVL树生成数组countSmaller[]。
步骤5 − 它包含每个数组元素右侧较小元素的个数。
步骤6 − 遍历数组和每个元素。
步骤7 − 检查它是否是到目前为止获得的最大值,并且countSmaller[i]是否大于或等于K。
步骤8 − 如果条件满足,则增加计数。
步骤9 − 打印最终计数作为答案。
步骤10 − 结束。
语法
for (i = k; i < array.length; i++){
minIndex = 0;
for (int j = 0; j < k; j++){
if(array[j] < array[minIndex]){
minIndex = j;
array[minIndex] = array[j];
}
}
if (array[minIndex] < array[i]){
int temp = array[minIndex];
array[minIndex] = array[i];
array[i] = temp;
}
这里有一个整数数组num和整数K。它将返回数组中的第K个元素。我们必须在O(n)时间复杂度内解决它。
方法
方法1 − 使用排序查找最大的K个(或最小的K个)元素。
方法2 − 高效的方法来查找数组中最大的K个(或最小的K个)元素。
使用排序查找最大的K个(或最小的K个)元素
使用排序方法,我们可以得到这个问题的结果。步骤如下:
降序排序元素。
打印排序数组中的前K个数字。
示例1
#include <bits/stdc++.h>
using namespace std;
void kLargest(int arr[], int a, int b){
sort(arr, arr + a, greater());
for (int i = 0; i < b; i++)
cout << arr[i] << " ";
}
int main(){
int arr[] = { 10, 16, 07, 2001, 1997, 2022, 50 };
int n = sizeof(arr) / sizeof(arr[0]);
int k = 3;
kLargest(arr, n, k);
}
输出
2022 2001 1997
高效的方法来查找数组中最大的K个(或最小的K个)元素
在这种方法中,我们将遵循以下步骤来找出结果:
开始。
从右到左遍历数组。
将所有元素插入AVL树。
生成数组countSmaller[]。
每个数组元素右侧较小元素的个数。
如果它是最大值,countSmaller[i]大于或等于K。
然后递增计数。
打印值。
结束。
示例2
#include <bits/stdc++.h>
using namespace std;
struct node {
int key;
struct node* left;
struct node* right;
int height;
int size;
};
int max(int a, int b);
int height(struct node* N){
if (N == NULL)
return 0;
return N->height;
}
int size(struct node* N){
if (N == NULL)
return 0;
return N->size;
}
int max(int a, int b){
return (a > b) ? a : b;
}
struct node* newNode(int key){
struct node* node
= (struct node*)
malloc(sizeof(struct node));
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1;
node->size = 1;
return (node);
}
struct node* rightRotate(struct node* y){
struct node* x = y->left;
struct node* T2 = x->right;
x->right = y;
y->left = T2;
y->height = max(height(y->left),
height(y->right))
+ 1;
x->height = max(height(x->left),
height(x->right))
+ 1;
y->size = size(y->left)
+ size(y->right) + 1;
x->size = size(x->left)
+ size(x->right) + 1;
return x;
}
struct node* leftRotate(struct node* x){
struct node* y = x->right;
struct node* T2 = y->left;
y->left = x;
x->right = T2;
x->height = max(height(x->left),
height(x->right))
+ 1;
y->height = max(height(y->left),
height(y->right))
+ 1;
x->size = size(x->left)
+ size(x->right) + 1;
y->size = size(y->left)
+ size(y->right) + 1;
return y;
}
int getBalance(struct node* N){
if (N == NULL)
return 0;
return height(N->left)
- height(N->right);
}
struct node* insert(struct node* node, int key,
int* count){
if (node == NULL)
return (newNode(key));
if (key < node->key)
node->left = insert(node->left, key, count);
else {
node->right = insert(node->right, key, count);
*count = *count + size(node->left) + 1;
}
node->height = max(height(node->left),
height(node->right))
+ 1;
node->size = size(node->left)
+ size(node->right) + 1;
int balance = getBalance(node);
if (balance > 1 && key < node->left->key)
return rightRotate(node);
if (balance < -1 && key > node->right->key)
return leftRotate(node);
if (balance > 1 && key > node->left->key) {
node->left = leftRotate(node->left);
return rightRotate(node);
}
if (balance < -1 && key < node->right->key) {
node->right = rightRotate(node->right);
return leftRotate(node);
}
return node;
}
void constructLowerArray(int arr[],
int countSmaller[],
int n){
int i, j;
struct node* root = NULL;
for (i = 0; i < n; i++)
countSmaller[i] = 0;
for (i = n - 1; i >= 0; i--) {
root = insert(root, arr[i], &countSmaller[i]);
}
}
int countElements(int A[], int n, int K){
int count = 0;
int* countSmaller = (int*)malloc(sizeof(int) * n);
constructLowerArray(A, countSmaller, n);
int maxi = INT_MIN;
for (int i = 0; i <= (n - K - 1); i++) {
if (A[i] > maxi && countSmaller[i] >= K) {
count++;
maxi = A[i];
}
}
return count;
}
int main(){
int A[] = { 16, 10, 2022, 1997, 7, 2001, 0 };
int n = sizeof(A) / sizeof(int);
int K = 3;
cout << countElements(A, n, K);
return 0;
}
输出
2
结论
因此,我们了解了如何编写C++代码来统计数组中大于其左侧所有元素且大于其右侧至少K个元素的元素个数。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP