字符串是一个对象,它表示数据字符的序列。字符串是始终表示为文本格式的数据容器。它还用于概念、比较、拆分、连接、替换、修剪、长度、实习、等于、比较、子字符串操作。使用快速排序分区算法的数组中的 k 个最大(或最小)元素。
这是一个数组 r[],其中包含 n 个不同的整数。任务是找到那个特定元素,该元素严格大于其前面的所有元素,并且严格大于其右侧的至少 k 个元素。该问题指出,一个由 n 个不同元素和整数 k 组成的数组 arr[ ](数组 r);我们必须找出比其左侧所有元素都大的元素个数,并且其右侧至少有 k 个元素。
input: r[] = {16,07,10,22,2001,1997}, k = 3output: 16therefore, the count is 2.
有两种方法可以找出大于左侧所有元素且右侧至少 k 个元素的数组元素。
朴素方法 - 这是遍历特定数组的最简单方法。在这个方法中,我们必须向左侧遍历所有元素来检查它是否较小。否则,向右遍历,检查最小的 k 个元素是否较小。对于这种方法,时间复杂度为 o(n2),辅助空间为 o(1)。
高效方法 - 这是一个可以通过自平衡 bst 完成的优化过程。通过avl tree从右向左一一遍历数组。 avl tree 生成一个数组 countsmaller[]。这里时间复杂度是o(nlogn),辅助空间是o(n)。对于满足满足条件的每个元素,增加计数。
让我们找出数组元素的数量大于其左侧所有元素且右侧至少有 k 个元素。
计算大于所有元素的数组元素的算法:-在此算法中,我们将按照一步一步的过程来计算数组元素的数量。通过这个,我们将构建一些 c++ 代码来找到所有元素中最大的元素。
第 1 步 - 开始。
第 2 步 - 从右向左遍历数组。
第 3 步 - 将所有元素插入 avl 树中。
第 4 步 - 通过使用 avl 树生成数组 countsmaller[]。
第 5 步 - 它包含每个数组元素右侧较小元素的计数。
第 6 步 - 遍历数组并查找每个元素。
第 7 步 - 检查是否是迄今为止获得的最大值,并且 countsmaller[i] 是否大于或等于 k。
第 8 步 - 如果满足条件,则增加计数。
第 9 步 - 打印 count 的最终值作为答案。
第 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 个最大(或最小)元素。
方法 2 - 查找数组中 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 个最大(或最小)元素的有效方法在此方法中,我们将按照以下步骤找出结果 -
开始。
从右向左遍历数组。
插入 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 个元素。
以上就是c++程序,用于计算数组元素大于其左侧所有元素且至少有k个元素在其右侧的数量的详细内容。