给定一个由N ( > 2 ) 个整数组成的数组arr[] ,任务是找到数组的峰值索引。如果数组不包含任何峰值索引,则打印-1 。
The peak index, say idx, of the given array arr[] consisting of N integers is defined as:
- 0 < idx < N – 1
- arr[0] < arr[1] < arr[2] < …. < arr[idx] < …. < arr[N – 2] < arr[N – 1]
例子:
Input: arr[] = {0, 1, 0}
Output: 1
Explanation: In the given array at index 1, arr[0] < arr[1] and arr[1] > arr[2]. Since index 1 satisfies the given condition, therefore 1 is a peak index of the array.
Input: arr[] = {3, 5, 5, 4, 3, 2, 1}
Output: -1
朴素的方法:最简单的方法是遍历数组并检查每个索引,比如idx ,在[1, N – 2]范围内索引idx是否可以是数组的峰值索引。这可以通过检查此索引idx左侧和右侧的所有元素是否必须严格按递增和递减顺序来完成。检查所有索引后,如果存在任何此类索引,则打印该索引。否则,打印“-1” 。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效方法:上述方法也可以基于以下事实进行优化:只有当数组包含严格递增的前缀和严格递减的后缀时,峰值索引才会存在。
请按照以下步骤解决问题:
- 初始化两个变量,比如ans ,以存储数组的峰值元素的索引。
- 在索引[1, N – 2]的范围内遍历给定数组,使用变量i 。如果arr[i]的值大于或等于arr[i + 1] ,则将ans更新为i并跳出循环。
- 如果ans 的值为0或(N – 1) ,则打印“-1” ,因为给定数组不存在这样的峰值索引。
- 现在,使用变量i在[ans, N – 2]范围内遍历给定数组,如果arr[i] 的值小于或等于arr[i + 1] ,则跳出循环。
- 完成上述步骤后,如果i 的值为(N – 1),则打印ans的值作为结果峰值索引。否则,打印“-1”,因为给定数组不存在这样的峰值索引。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the peak
// index for the given array
int peakIndex(int arr[], int N)
{
// Base Case
if (N < 3)
return -1;
int i = 0;
// Check for strictly
// increasing array
while (i + 1 < N)
{
// If the strictly increasing
// condition is violated, then break
if (arr[i + 1] < arr[i] ||
arr[i] == arr[i + 1])
break;
i++;
}
if (i == 0 || i == N - 1)
return -1;
// Stores the value of i, which
// is a potential peak index
int ans = i;
// Second traversal, for
// strictly decreasing array
while (i < N - 1)
{
// When the strictly
// decreasing condition is
// violated, then break
if (arr[i] < arr[i + 1] ||
arr[i] == arr[i + 1])
break;
i++;
}
// If i = N - 1, it means that
// ans is the peak index
if (i == N - 1)
return ans;
// Otherwise, peak index doesn't exist
return -1;
}
// Driver Code
int main()
{
int arr[] = { 0, 1, 0 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << peakIndex(arr, N) << "\n";
return 0;
}
// This code is contributed by Kingash
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG {
// Function to find the peak
// index for the given array
public static int peakIndex(int[] arr)
{
int N = arr.length;
// Base Case
if (arr.length < 3)
return -1;
int i = 0;
// Check for strictly
// increasing array
while (i + 1 < N) {
// If the strictly increasing
// condition is violated, then break
if (arr[i + 1] < arr[i]
|| arr[i] == arr[i + 1])
break;
i++;
}
if (i == 0 || i == N - 1)
return -1;
// Stores the value of i, which
// is a potential peak index
int ans = i;
// Second traversal, for
// strictly decreasing array
while (i < N - 1) {
// When the strictly
// decreasing condition is
// violated, then break
if (arr[i] < arr[i + 1]
|| arr[i] == arr[i + 1])
break;
i++;
}
// If i = N - 1, it means that
// ans is the peak index
if (i == N - 1)
return ans;
// Otherwise, peak index doesn't exist
return -1;
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 0, 1, 0 };
System.out.println(peakIndex(arr));
}
}
Python3
# Python3 program for the above approach
# Function to find the peak
# index for the given array
def peakIndex(arr):
N = len(arr)
# Base Case
if (len(arr) < 3):
return -1
i = 0
# Check for strictly
# increasing array
while (i + 1 < N):
# If the strictly increasing
# condition is violated, then break
if (arr[i + 1] < arr[i] or
arr[i] == arr[i + 1]):
break
i += 1
if (i == 0 or i == N - 1):
return -1
# Stores the value of i, which
# is a potential peak index
ans = i
# Second traversal, for
# strictly decreasing array
while (i < N - 1):
# When the strictly
# decreasing condition is
# violated, then break
if (arr[i] < arr[i + 1] or
arr[i] == arr[i + 1]):
break
i += 1
# If i = N - 1, it means that
# ans is the peak index
if (i == N - 1):
return ans
# Otherwise, peak index doesn't exist
return -1
# Driver Code
if __name__ == '__main__':
arr = [0, 1, 0]
print(peakIndex(arr))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the peak
// index for the given array
public static int peakIndex(int[] arr)
{
int N = arr.Length;
// Base Case
if (arr.Length < 3)
return -1;
int i = 0;
// Check for strictly
// increasing array
while (i + 1 < N)
{
// If the strictly increasing
// condition is violated, then break
if (arr[i + 1] < arr[i] ||
arr[i] == arr[i + 1])
break;
i++;
}
if (i == 0 || i == N - 1)
return -1;
// Stores the value of i, which
// is a potential peak index
int ans = i;
// Second traversal, for
// strictly decreasing array
while (i < N - 1)
{
// When the strictly
// decreasing condition is
// violated, then break
if (arr[i] < arr[i + 1] ||
arr[i] == arr[i + 1])
break;
i++;
}
// If i = N - 1, it means that
// ans is the peak index
if (i == N - 1)
return ans;
// Otherwise, peak index doesn't exist
return -1;
}
// Driver Code
static public void Main()
{
int[] arr = { 0, 1, 0 };
Console.WriteLine(peakIndex(arr));
}
}
// This code is contributed by splevel62
Javascript
1
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live