计数器数组
Problem statement:
问题陈述:
Given an array of N positive integers a1, a2, ..., an. The value of each contiguous subarray of a given array is the maximum element present in that subarray. The task is to return the number of subarrays having value strictly greater than K.
给定N个正整数数组a 1 ,a 2 ,...,a n 。 给定数组的每个连续子数组的值是该子数组中存在的最大元素。 任务是返回值严格大于K的子数组数。
Input:
输入:
The first line contains two space-separated positive integers N and K denoting the size of the array and the value of K. The second line contains N space-separated positive integers denoting the elements of the array.
第一行包含两个空间隔开的正整数N,和K表示的阵列的尺寸和K值。 第二行包含N个以空格分隔的正整数,它们表示数组的元素。
Output:
输出:
Output the number of subarrays having value strictly greater than K.
输出值严格大于K的子数组数。
Constraints:
限制条件:
1 <= T <= 50
1 <= N <= 100
1 <= a[i] <= 105
Example:
例:
Test case: 1
Input:
5 3
3 4 5 2 7
Output:
13
Test case: 2
4 1
1 2 3 4
Output:
9
Explanation:
说明:
Test case 1:
All possible subarrays are listed below
with their respective value (maximum in the subarray)
3 -> 3
4 -> 4
5 -> 5
2 -> 2
7 -> 7
3, 4 -> 4
4, 5 -> 5
5, 2 -> 5
2, 7 -> 7
3, 4, 5 -> 5
4, 5, 2 -> 5
5, 2, 7 -> 7
3, 4, 5, 2 -> 5
4, 5, 2, 7 -> 7
3, 4, 5, 2, 7 -> 7
So, number of valid subarrays is 13
Solution Approach:
解决方法:
Create dp[n][n] to store value (maximum) of subarray;
创建dp [n] [n]来存储子数组的值(最大值);
Initialize count = 0 which will be our final result;
初始化计数= 0 ,这将是我们的最终结果;
Base case computation (single length subarrays),
基本案例计算(单长度子数组),
for i=0 to n // since only one element in single length subarray, // just check for that element if(a[i]>k) dp[i][i]=a[i]; count++; else dp[i][i]=-1; // or arr[i] itslef end for
Computing all length subarray cases,
计算所有长度的子阵列案例,
for subarray length,len=2 to n for start=0 to n-len end=start+len-1; // last element of subarray if(a[end]>k || dp[start][end-1]>k) dp[start][end]=std::max(a[end],dp[start][end-1]); count++; else dp[start][end]=-1; end for end for
Okay, so the strategy is to compute for subarray a[start..end] with help of already computed one a[start..end-1].
好的,因此该策略是在已经计算的a [start..end-1]的帮助下为子数组a [start..end]计算。
Subarray a[start..end] will only make a count if a[start..end-1] already makes a count ( yes because, it's part of the subarray so, anything max in it would be max in the parent one too) Or if a[end]>k.
子数组a [start..end]仅在a [start..end-1]已经计数的情况下才会计数(是的,因为它是子数组的一部分,所以其中的任何最大值在父数组中也是最大值)或者a [end]> k 。
In both the cases maximum of the subarray, a[start..end] is greater than K
在这两种情况下,子数组的最大值a [start..end]都大于K
That's what we have done.
那就是我们所做的。
Below is illustration for our test case 1,
下面是我们的测试用例1的说明,
Input array: [3, 4, 5, 2, 7] and K=3.
输入数组: [3,4,5,2,7]和K = 3 。
So, let's compute the basic test case first
因此,让我们先计算基本测试用例
We need a 5X5 DP array for this
为此,我们需要一个5X5 DP阵列
Starting to compute for other values.
开始计算其他值。
Len=2
Len = 2
Start=0, end=1
开始= 0,结束= 1
Start=1, end=2
开始= 1,结束= 2
Start=2, end=3
开始= 2,结束= 3
Start=3, end=4
开始= 3,结束= 4
Len=3
Len = 3
Start=0, end=2
开始= 0,结束= 2
Start=1, end=3
开始= 1,结束= 3
Start=2, end=4
开始= 2,结束= 4
Len=4
Len = 4
Start=0, end=3
开始= 0,结束= 3
Start=1, end=4
开始= 1,结束= 4
Len=5
Len = 5
Start=0, end=3
开始= 0,结束= 3
Done!
做完了!
Count is 13 (count of positive values in the array).
Count是13(数组中正值的数量)。
C++ Implementation:
C ++实现:
#include <bits/stdc++.h>
using namespace std;
int maximum(int a, int b)
{
return (a > b) ? a : b;
}
int subArray(vector<int> a, int n, int k)
{
int dp[n][n];
int count = 0;
for (int i = 0; i < n; i++) {
if (a[i] > k) {
dp[i][i] = a[i];
count++;
}
else
dp[i][i] = -1; //or arr[i] itslef
}
for (int len = 2; len <= n; len++) {
for (int start = 0; start <= n - len; start++) {
int end = start + len - 1;
if (a[end] > k || dp[start][end - 1] > k) {
dp[start][end] = std::max(a[end], dp[start][end - 1]);
count++;
}
else
dp[start][end] = -1;
}
}
return count;
}
int main()
{
int n, item, k;
cout << "Input size of array\n";
cin >> n;
cout << "Input k\n";
cin >> k;
cout << "Add the array elements\n";
vector<int> a;
for (int j = 0; j < n; j++) {
scanf("%d", &item);
a.push_back(item);
}
cout << "Total count of valid subarray is " << subArray(a, n, k) << endl;
return 0;
}
Output:
输出:
Input size of array
5
Input k
3
Add the array elements
3 4 5 2 7
Total count of valid subarray is 13
翻译自: https://www.includehelp.com/icp/count-of-subarrays.aspx
计数器数组