数组中位置 j 的计数,使得 arr[i] 在索引范围 [i, j] 中最大,端点相同
给定一个由N个正整数组成的数组arr[] ,任务是找到所有j使得arr[j] = arr[i]以及范围[min(j, i), max(j, i)]小于或等于arr[i]其中1 ≤ i ≤ N 。
例子:
Input: arr[] = {4, 7, 7, 9, 7}, N = 5
Output: 1 2 2 1 1
Explanation:
For i = 1, j = 1 is the only element such that arr[i] = arr[j] and no element in the middle has value greater than arr[1].
For i = 2, j = 2 and 3 are the elements such that arr[i] = arr[j] and no elements in the middle has value greater than arr[2].
For i = 3, j = 2 and 3 are the elements such that arr[i] = arr[j] and no elements in the middle has value greater than arr[3].
For i = 4, j = 4 is the only element such that arr[i] = arr[j] and no element in the middle has value greater than arr[4].
For i = 5, j = 5 is the only element such that arr[i] = arr[j] and no element in the middle has value greater than arr[5].
Input: arr[] = {1, 2, 1, 2, 4}, N = 5
Output: 1 2 1 2 1
方法:解决问题最简单的方法是使用两个嵌套的for循环遍历数组,找到满足arr[i] = arr[j]且[i, j]范围内没有元素大于arr的对[我] 。请按照以下步骤解决问题:
- 初始化一个数组,比如ans[] ,它存储范围[0, N-1]中所有元素的答案。
- 使用变量i在[N-1, 0]范围内迭代并执行以下步骤:
- 使用变量j在范围[i, 0]中迭代并执行以下步骤:
- 如果arr[j] = arr[i] ,则将ans[i]的值增加1 。
- 如果arr[j] > arr[i] ,则终止循环。
- 使用变量j在[i+1, N-1]范围内迭代并执行以下步骤:
- 如果arr[j] = arr[i] ,则将ans[i]的值增加1 。
- 如果arr[j] > arr[i] ,则终止循环。
- 使用变量j在范围[i, 0]中迭代并执行以下步骤:
- 完成上述步骤后,打印数组ans[]作为答案。
下面是上述方法的实现
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find j such that arr[j]=arr[i]
// and no element is greater than arr[i] in the
// range [j, i]
void findElements(int arr[], int N)
{
// To Store answer
int ans[N];
// Initialize the value of ans[i] as 0
for (int i = 0; i < N; i++) {
ans[i] = 0;
}
// Traverse the array in reverse direction
for (int i = N - 1; i >= 0; i--) {
// Traverse in the range [i, 0]
for (int j = i; j >= 0; j--) {
if (arr[j] == arr[i]) {
// Increment ans[i] if arr[j] =arr[i]
ans[i]++;
}
else
// Terminate the loop
if (arr[j] > arr[i])
break;
}
// Traverse in the range [i+1, N-1]
for (int j = i + 1; j < N; j++) {
// Increment ans[i] if arr[i] = arr[j]
if (arr[j] == arr[i])
ans[i]++;
else if (arr[j] > arr[i]) {
break;
}
}
}
for (int i = 0; i < N; i++) {
cout << ans[i] << " ";
}
}
// Driver Code
int main()
{
// Given Input
int arr[] = { 1, 2, 1, 2, 4 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
findElements(arr, N);
}
Java
// Java program for the above approach
public class GFG
{
// Function to find j such that arr[j]=arr[i]
// and no element is greater than arr[i] in the
// range [j, i]
static void findElements(int arr[], int N)
{
// To Store answer
int ans[] = new int[N];
// Initialize the value of ans[i] as 0
for (int i = 0; i < N; i++) {
ans[i] = 0;
}
// Traverse the array in reverse direction
for (int i = N - 1; i >= 0; i--) {
// Traverse in the range [i, 0]
for (int j = i; j >= 0; j--) {
if (arr[j] == arr[i]) {
// Increment ans[i] if arr[j] =arr[i]
ans[i]++;
}
else
// Terminate the loop
if (arr[j] > arr[i])
break;
}
// Traverse in the range [i+1, N-1]
for (int j = i + 1; j < N; j++)
{
// Increment ans[i] if arr[i] = arr[j]
if (arr[j] == arr[i])
ans[i]++;
else if (arr[j] > arr[i]) {
break;
}
}
}
for (int i = 0; i < N; i++) {
System.out.print(ans[i] + " ");
}
}
// Driver code
public static void main(String[] args)
{
// Given Input
int arr[] = { 1, 2, 1, 2, 4 };
int N = arr.length;
// Function Call
findElements(arr, N);
}
}
// This code is contributed by abhinavjain194
Python3
# Python3 program for the above approach
# Function to find j such that arr[j]=arr[i]
# and no element is greater than arr[i] in the
# range [j, i]
def findElements(arr, N):
# Initialising a list to store ans
# Initialize the value of ans[i] as 0
ans = [0 for i in range(N)]
# Traverse the array in reverse direction
for i in range(N - 1, -1, -1):
# Traverse in the range [i, 0]
for j in range(i, -1, -1):
if arr[j] == arr[i]:
# Increment ans[i] if arr[j] = arr[i]
ans[i] += 1
else:
# Terminate the loop
if arr[j] > arr[i]:
break
# Traverse in the range [i+1, N-1]
for j in range(i+1, N):
# Increment ans[i] if arr[j] = arr[i]
if arr[j] == arr[i]:
ans[i] += 1
else:
if arr[j] > arr[i]:
break
# Print the ans
for i in range(N):
print(ans[i], end = " ")
return
# Driver Code
if __name__ == '__main__':
# Given Input
arr = [ 1, 2, 1, 2, 4 ]
N = len(arr)
# Function call
findElements(arr, N)
# This code is contributed by MuskanKalra1
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find j such that arr[j]=arr[i]
// and no element is greater than arr[i] in the
// range [j, i]
static void findElements(int []arr, int N)
{
// To Store answer
int []ans = new int[N];
// Initialize the value of ans[i] as 0
for(int i = 0; i < N; i++)
{
ans[i] = 0;
}
// Traverse the array in reverse direction
for(int i = N - 1; i >= 0; i--)
{
// Traverse in the range [i, 0]
for(int j = i; j >= 0; j--)
{
if (arr[j] == arr[i])
{
// Increment ans[i] if arr[j] =arr[i]
ans[i]++;
}
else
// Terminate the loop
if (arr[j] > arr[i])
break;
}
// Traverse in the range [i+1, N-1]
for(int j = i + 1; j < N; j++)
{
// Increment ans[i] if arr[i] = arr[j]
if (arr[j] == arr[i])
ans[i]++;
else if (arr[j] > arr[i])
{
break;
}
}
}
for(int i = 0; i < N; i++)
{
Console.Write(ans[i] + " ");
}
}
// Driver Code
public static void Main()
{
// Given Input
int []arr = { 1, 2, 1, 2, 4 };
int N = arr.Length;
// Function Call
findElements(arr, N);
}
}
// This code is contributed by SURENDRA_GANGWAR
Javascript
1 2 1 2 1
时间复杂度: O(N 2 )
辅助空间: O(N)