查找之前至少有 K 个非递增元素和之后至少有 K 个非递减元素的索引
给定一个大小为N的数组arr[]和一个整数K ,任务是找到给定数组中的所有索引,这些索引之前至少有K个非递增元素,之后至少有K个非递减元素。
例子:
Input: arr[] = {1, 1, 1, 1, 1}, K = 0
Output: 0 1 2 3 4
Explanation: Since K equals 0, every index satisfies the condition.
Input: arr[] = {1, 2, 3, 4, 5, 6}, K = 2
Output: -1
Explanation: No index has 2 non-increasing before it and 2 non-decreasing elements after it.
方法:可以通过使用前缀和后缀数组的概念找到解决方案。请按照以下步骤操作:
- 形成prefix[]数组,其中prefix[i]表示i之前的元素个数,服从非递增顺序。
- 形成suffix[]数组,其中suffix[i]表示i之后的元素个数,且服从非递减顺序。
- 现在只有索引应该包含在 answer 中, prefix[i]和suffix[i]都大于或等于K 。
下面是上述方法的实现。
C++
// C++ code for the above approach
#include
using namespace std;
// Function to find all the indices
vector findIndices(int arr[], int K,
int N)
{
vector prefix(N), suffix(N);
vector ans;
prefix[0] = 0;
for (int i = 1; i < N; i++) {
if (arr[i] <= arr[i - 1])
prefix[i] = prefix[i - 1] + 1;
else
prefix[i] = 0;
}
suffix[N - 1] = 0;
for (int i = N - 2; i >= 0; i--) {
if (arr[i] <= arr[i + 1])
suffix[i] = suffix[i + 1] + 1;
else
suffix[i] = 0;
}
for (int i = 0; i < N; i++) {
if (prefix[i] >= K && suffix[i] >= K)
ans.push_back(i);
}
return ans;
}
// Driver code
int main()
{
int arr[] = { 1, 1, 1, 1, 1 };
int K = 0;
int N = sizeof(arr) / sizeof(arr[0]);
vector ans = findIndices(arr, K, N);
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
if (ans.size() == 0)
cout << "-1";
return 0;
}
Java
// Java code for the above approach
import java.util.ArrayList;
class GFG {
// Function to find all the indices
static ArrayList findIndices(int[] arr, int K, int N)
{
int[] prefix = new int[N];
int[] suffix = new int[N];
ArrayList ans = new ArrayList();
prefix[0] = 0;
for (int i = 1; i < N; i++) {
if (arr[i] <= arr[i - 1])
prefix[i] = prefix[i - 1] + 1;
else
prefix[i] = 0;
}
suffix[N - 1] = 0;
for (int i = N - 2; i >= 0; i--) {
if (arr[i] <= arr[i + 1])
suffix[i] = suffix[i + 1] + 1;
else
suffix[i] = 0;
}
for (int i = 0; i < N; i++) {
if (prefix[i] >= K && suffix[i] >= K)
ans.add(i);
}
return ans;
}
// Driver code
public static void main(String args[])
{
int[] arr = { 1, 1, 1, 1, 1 };
int K = 0;
int N = arr.length;
ArrayList ans = findIndices(arr, K, N);
for (int i = 0; i < ans.size(); i++) {
System.out.print(ans.get(i) + " ");
}
if (ans.size() == 0)
System.out.println("-1");
}
}
// This code is contributed by gfgking
Python3
# Python code for the above approach
# Function to find all the indices
def findIndices (arr, K, N):
prefix = [0] * N
suffix = [0] * N
ans = [];
prefix[0] = 0;
for i in range(1, N):
if (arr[i] <= arr[i - 1]):
prefix[i] = prefix[i - 1] + 1;
else:
prefix[i] = 0;
suffix[N - 1] = 0;
for i in range(N - 2, 1, -1):
if (arr[i] <= arr[i + 1]):
suffix[i] = suffix[i + 1] + 1;
else:
suffix[i] = 0;
for i in range(N):
if (prefix[i] >= K and suffix[i] >= K):
ans.append(i);
return ans;
# Driver code
arr = [1, 1, 1, 1, 1];
K = 0;
N = len(arr)
ans = findIndices(arr, K, N);
for i in range(len(ans)):
print(ans[i], end=" ");
if (len(ans) == 0):
print("-1");
# This code is contributed by Saurabh Jaiswal
C#
// C# code for the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to find all the indices
static List findIndices(int[] arr, int K, int N)
{
int[] prefix = new int[N];
int[] suffix = new int[N];
List ans = new List();
prefix[0] = 0;
for (int i = 1; i < N; i++) {
if (arr[i] <= arr[i - 1])
prefix[i] = prefix[i - 1] + 1;
else
prefix[i] = 0;
}
suffix[N - 1] = 0;
for (int i = N - 2; i >= 0; i--) {
if (arr[i] <= arr[i + 1])
suffix[i] = suffix[i + 1] + 1;
else
suffix[i] = 0;
}
for (int i = 0; i < N; i++) {
if (prefix[i] >= K && suffix[i] >= K)
ans.Add(i);
}
return ans;
}
// Driver code
public static void Main()
{
int[] arr = { 1, 1, 1, 1, 1 };
int K = 0;
int N = arr.Length;
List ans = findIndices(arr, K, N);
for (int i = 0; i < ans.Count; i++) {
Console.Write(ans[i] + " ");
}
if (ans.Count == 0)
Console.Write("-1");
}
}
// This code is contributed by ukasp
Javascript
输出
0 1 2 3 4
时间复杂度: 在)
辅助空间: O(N)