给定大小为N的数组arr [] ,任务是查找最大数量小于其余后缀数组中最大元素的前缀数组的数量。
例子:
Input: arr[] = {2, 3, 4, 8, 1, 4}
Output: 3
Explanation:
Prefix array = {2}, {2, 3}, {2, 3, 4}, {2, 3, 4, 8}, {2, 3, 4, 8, 1}
Respective Suffix = {3, 4, 8, 1, 4}, {4, 8, 1, 4}, {8, 1, 4}, {1, 4}, {4}
Only the first 3 the prefix arrays have maximum valued element less than the maximum value element in the respective suffix array.
Input: arr[] = {4, 4, 4, 4, 5}
Output: 4
天真的方法:最简单的方法是查找给定数组的所有可能的前缀和后缀,并对前缀数组中最大元素小于后缀数组中最大元素的索引数进行计数。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效方法:为了优化上述方法,其思想是存储数组的每个前缀和后缀的最大值,然后计算最大值小于其相应后缀数组的前缀数组的数量。请按照以下步骤解决问题:
- 初始化一个变量,例如ans和两个数组N的prefix []和suffix [] 。
- 遍历数组arr []的范围为[0,N – 1] ,对于prefix []中的每个第i个索引,将最大元素存储为prefix [i] = max(prefix [i – 1],arr [i] ) 。
- 在[N – 1,0]范围内以相反的顺序遍历给定数组,对于suffix []中的每个第i个索引,将最大元素存储为suffix [i] = max(suffix [i + 1],arr [ i]) 。
- 现在,遍历数组arr []的范围为[0,N – 2] ,如果prefix [i]小于后缀[i] ,则将ans加1 。
- 完成上述步骤后,输出ans的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to print the count of indices
// in which the maximum in prefix arrays
// is less than that in the suffix array
void count(int a[], int n)
{
// If size of array is 1
if (n == 1) {
cout << 0;
return;
}
// pre[]: Prefix array
// suf[]: Suffix array
int pre[n - 1], suf[n - 1];
int max = a[0];
// Stores the required count
int ans = 0, i;
pre[0] = a[0];
// Find the maximum in prefix array
for (i = 1; i < n - 1; i++) {
if (a[i] > max)
max = a[i];
pre[i] = max;
}
max = a[n - 1];
suf[n - 2] = a[n - 1];
// Find the maximum in suffix array
for (i = n - 2; i >= 1; i--) {
if (a[i] > max)
max = a[i];
suf[i - 1] = max;
}
// Traverse the array
for (i = 0; i < n - 1; i++) {
// If maximum in prefix array
// is less than maximum in
// the suffix array
if (pre[i] < suf[i])
ans++;
}
// Print the answer
cout << ans;
}
// Driver Code
int main()
{
int arr[] = { 2, 3, 4, 8, 1, 4 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
count(arr, N);
return 0;
}
Java
// Java program for the above approach
public class gfg
{
// Function to print the count of indices
// in which the maximum in prefix arrays
// is less than that in the suffix array
static void count(int a[], int n)
{
// If size of array is 1
if (n == 1)
{
System.out.print(0);
return;
}
// pre[]: Prefix array
// suf[]: Suffix array
int[] pre = new int[n - 1];
int[] suf = new int[n - 1];
int max = a[0];
// Stores the required count
int ans = 0, i;
pre[0] = a[0];
// Find the maximum in prefix array
for(i = 1; i < n - 1; i++)
{
if (a[i] > max)
max = a[i];
pre[i] = max;
}
max = a[n - 1];
suf[n - 2] = a[n - 1];
// Find the maximum in suffix array
for(i = n - 2; i >= 1; i--)
{
if (a[i] > max)
max = a[i];
suf[i - 1] = max;
}
// Traverse the array
for(i = 0; i < n - 1; i++)
{
// If maximum in prefix array
// is less than maximum in
// the suffix array
if (pre[i] < suf[i])
ans++;
}
// Print the answer
System.out.print(ans);
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 2, 3, 4, 8, 1, 4 };
int N = arr.length;
// Function Call
count(arr, N);
}
}
// This code is contributed by divyesh072019.
Python3
# Python program for the above approach
# Function to print the count of indices
# in which the maximum in prefix arrays
# is less than that in the suffix array
def count(a, n) :
# If size of array is 1
if (n == 1) :
print(0)
return
# pre[]: Prefix array
# suf[]: Suffix array
pre = [0] * (n - 1)
suf = [0] * (n - 1)
max = a[0]
# Stores the required count
ans = 0
pre[0] = a[0]
# Find the maximum in prefix array
for i in range(n-1):
if (a[i] > max):
max = a[i]
pre[i] = max
max = a[n - 1]
suf[n - 2] = a[n - 1]
# Find the maximum in suffix array
for i in range(n-2, 0, -1):
if (a[i] > max):
max = a[i]
suf[i - 1] = max
# Traverse the array
for i in range(n - 1):
# If maximum in prefix array
# is less than maximum in
# the suffix array
if (pre[i] < suf[i]) :
ans += 1
# Print the answer
print(ans)
# Driver Code
arr = [ 2, 3, 4, 8, 1, 4 ]
N = len(arr)
# Function Call
count(arr, N)
# This code is contributed by code_hunt.
C#
// C# program for the above approach
using System;
class GFG{
// Function to print the count of indices
// in which the maximum in prefix arrays
// is less than that in the suffix array
static void count(int[] a, int n)
{
// If size of array is 1
if (n == 1)
{
Console.Write(0);
return;
}
// pre[]: Prefix array
// suf[]: Suffix array
int[] pre = new int[n - 1];
int[] suf = new int[n - 1];
int max = a[0];
// Stores the required count
int ans = 0, i;
pre[0] = a[0];
// Find the maximum in prefix array
for(i = 1; i < n - 1; i++)
{
if (a[i] > max)
max = a[i];
pre[i] = max;
}
max = a[n - 1];
suf[n - 2] = a[n - 1];
// Find the maximum in suffix array
for(i = n - 2; i >= 1; i--)
{
if (a[i] > max)
max = a[i];
suf[i - 1] = max;
}
// Traverse the array
for(i = 0; i < n - 1; i++)
{
// If maximum in prefix array
// is less than maximum in
// the suffix array
if (pre[i] < suf[i])
ans++;
}
// Print the answer
Console.Write(ans);
}
// Driver code
static void Main()
{
int[] arr = { 2, 3, 4, 8, 1, 4 };
int N = arr.Length;
// Function Call
count(arr, N);
}
}
// This code is contributed by divyeshrabadiya07
Javascript
输出:
3
时间复杂度: O(N)
辅助空间: O(N)