给定一个数组arr[]和三个整数X, Y, Z ,任务是从数组中找到X、Y和Z出现次数相等的子数组的数量。
例子:
Input: arr[] = {3, 6, 7, 8, 3, 6, 7}, X = 3, Y = 6, Z = 7
Output: 8
Explanationn: There are 8 such subarrays i.e. {3, 6, 7}, {6, 7, 8, 3}, {7, 8, 3, 6}, {8}, {3, 6, 7, 8}, {8, 3, 6, 7}, {3, 6, 7}, {3, 6, 7, 8, 3, 6, 7}, in which the count of occurrences of 3, 6 and 7 are equal.
Input: arr[] = {23, 45, 76, 45, 76, 87, 23}, X = 23, Y = 45, Z = 76
Output: 4
Explanation: There are 3 such subarrays i.e. {23, 45, 76}, {45, 76, 87, 23}, {87}, {23, 45, 76, 45, 76, 87, 23}, in which the count of occurrences of 23, 45 and 76 are equal.
处理方法:按照以下步骤解决问题:
- 初始化三个变量,比如fNum_count = 0、sNum_count = 0 和 tNum_count = 0和mini 。
- 初始化一个 Map , int>, int>。
- {0, 0, 0} 的递增频率一次。
- 遍历数组,如果找到任何给定的数字,则增加其对应的计数并从所有这些数字中减少三个中的最小值。
- 遍历后,增加这三个变量的值集的频率。
- 现在初始化一个变量,比如final_ans。
- 遍历地图并将每个频率的v*(v-1) / 2添加到 final_ans 。
- 打印final_ans作为必需的答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
typedef long long ll;
// Function to count subarrays
void countSubarrays(int arr[], int N,
int X, int Y, int Z)
{
map, int>, int> m;
m[{ { 0, 0 }, 0 }]++;
int fNum_count = 0, sNum_count = 0,
tNum_count = 0;
int mini;
// Traverse the array
for (int i = 0; i < N; ++i) {
// Check is arr[i] is equal to X
if (arr[i] == X) {
// Increment fNum_count
fNum_count++;
mini = min(min(fNum_count,
sNum_count),
tNum_count);
fNum_count -= mini;
sNum_count -= mini;
tNum_count -= mini;
}
// Check is arr[i] is equal to Y
else if (arr[i] == Y) {
// Increment the count of sNum_count
sNum_count++;
mini = min(min(fNum_count,
sNum_count),
tNum_count);
fNum_count -= mini;
sNum_count -= mini;
tNum_count -= mini;
}
// Check is arr[i] is equal to Z
else if (arr[i] == Z) {
// Increment the count of
// tNum_count
tNum_count++;
mini = min(min(fNum_count,
sNum_count),
tNum_count);
fNum_count -= mini;
sNum_count -= mini;
tNum_count -= mini;
}
m[{ { fNum_count, sNum_count },
tNum_count }]++;
}
ll final_ans = 0;
map, int>,
int>::iterator it;
// Iterate over the map
for (it = m.begin(); it != m.end();
++it) {
ll val = it->second;
final_ans += (val * (val - 1)) / 2;
}
// Print the answer
cout << final_ans;
}
// Driver Code
int main()
{
// Given array
int arr[] = { 3, 6, 7, 8, 3, 6, 7 };
// Size of the array
int N = sizeof(arr) / sizeof(arr[0]);
// Given value of X, Y & Z
int X = 3, Y = 6, Z = 7;
// Function Call
countSubarrays(arr, N, X, Y, Z);
return 0;
}
Python3
# Python3 program for the above approach
# Function to count subarrays
def countSubarrays(arr, N, X, Y, Z):
m = {}
m[( ( 0, 0 ), 0 )] = 1
fNum_count, sNum_count = 0, 0
tNum_count = 0
mini = 0
# Traverse the array
for i in range(N):
# Check is arr[i] is equal to X
if (arr[i] == X):
# Increment fNum_count
fNum_count += 1
mini = min(min(fNum_count,sNum_count),tNum_count)
fNum_count -= mini
sNum_count -= mini
tNum_count -= mini
# Check is arr[i] is equal to Y
elif (arr[i] == Y):
# Increment the count of sNum_count
sNum_count+=1
mini = min(min(fNum_count,sNum_count), tNum_count)
fNum_count -= mini
sNum_count -= mini
tNum_count -= mini
# Check is arr[i] is equal to Z
elif (arr[i] == Z):
# Increment the count of
# tNum_count
tNum_count +=1
mini = min(min(fNum_count, sNum_count), tNum_count)
fNum_count -= mini
sNum_count -= mini
tNum_count -= mini
m[(( fNum_count, sNum_count ),tNum_count )] = m.get((( fNum_count, sNum_count ),tNum_count ),0)+1
final_ans = 0
# Iterate over the map
for it in m:
val = m[it]
final_ans += (val * (val - 1)) // 2
# Print the answer
print (final_ans)
# Driver Code
if __name__ == '__main__':
# Given array
arr = [3, 6, 7, 8, 3, 6, 7]
# Size of the array
N = len(arr)
# Given value of X, Y & Z
X, Y, Z = 3, 6, 7
# Function Call
countSubarrays(arr, N, X, Y, Z)
# This code is contributed by mohit kumar 29.
8
时间复杂度: O(N * LogN)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。