给定一个数组arr[] ,任务是选择一个大小为K的子数组,其中包含相对于相邻元素的最大谷点数。
元素 arr[i] 被称为谷点,如果它的两个相邻元素都大于它,即和 .
例子:
Input: arr[] = {5, 4, 6, 4, 5, 2, 3, 1}, K = 7 the
Output: 3
Explanation:
In subarray arr[0-6] = {5, 4, 6, 4, 5, 2, 3}
There are 3 Valley points in the subarray, which is maximum.
Input: arr[] = {2, 1, 4, 2, 3, 4, 1, 2}, K = 4
Output: 1
Explanation:
In subarray arr[0-3] = {2, 1, 4, 2}
There is only one valley point in the subarray, which is the maximum.
做法:思路是使用滑动窗口技术来解决这个问题。
下面是该方法步骤的说明:
- 求第一个大小为 K 的子数组中谷点的总数。
- 迭代所有可能子数组的起始点,即数组的NK个点,应用包含和排除原理计算当前窗口中谷点的数量。
- 在每一步,更新最终答案以计算每个子数组的全局最大值。
下面是上述方法的实现:
C++
// C++ implementation to find the
// maximum number of valley elements
// in the subarrays of size K
#include
using namespace std;
// Function to find the valley elements
// in the array which contains
// in the subarrays of the size K
void minpoint(int arr[],int n, int k)
{
int min_point = 0;
for (int i = 1; i < k-1 ; i++)
{
// Increment min_point
// if element at index i
// is smaller than element
// at index i + 1 and i-1
if(arr[i] < arr[i - 1] && arr[i] < arr[i + 1])
min_point += 1;
}
// final_point to maintain maximum
// of min points of subarray
int final_point = min_point;
// Iterate over array
// from kth element
for(int i = k ; i < n; i++)
{
// Leftmost element of subarray
if(arr[i - ( k - 1 )] < arr[i - ( k - 1 ) + 1]&&
arr[i - ( k - 1 )] < arr[i - ( k - 1 ) - 1])
min_point -= 1;
// Rightmost element of subarray
if(arr[i - 1] < arr[i] && arr[i - 1] < arr[i - 2])
min_point += 1;
// if new subarray have greater
// number of min points than previous
// subarray, then final_point is modified
if(min_point > final_point)
final_point = min_point;
}
// Max minimum points in
// subarray of size k
cout<<(final_point);
}
// Driver Code
int main()
{
int arr[] = {2, 1, 4, 2, 3, 4, 1, 2};
int n = sizeof(arr)/sizeof(arr[0]);
int k = 4;
minpoint(arr, n, k);
return 0;
}
// This code contributed by chitranayal
Java
// Java implementation to find the
// maximum number of valley elements
// in the subarrays of size K
class GFG{
// Function to find the valley elements
// in the array which contains
// in the subarrays of the size K
static void minpoint(int arr[], int n, int k)
{
int min_point = 0;
for(int i = 1; i < k - 1; i++)
{
// Increment min_point
// if element at index i
// is smaller than element
// at index i + 1 and i-1
if(arr[i] < arr[i - 1] &&
arr[i] < arr[i + 1])
min_point += 1;
}
// final_point to maintain maximum
// of min points of subarray
int final_point = min_point;
// Iterate over array
// from kth element
for(int i = k ; i < n; i++)
{
// Leftmost element of subarray
if(arr[i - ( k - 1 )] < arr[i - ( k - 1 ) + 1] &&
arr[i - ( k - 1 )] < arr[i - ( k - 1 ) - 1])
min_point -= 1;
// Rightmost element of subarray
if(arr[i - 1] < arr[i] &&
arr[i - 1] < arr[i - 2])
min_point += 1;
// If new subarray have greater
// number of min points than previous
// subarray, then final_point is modified
if(min_point > final_point)
final_point = min_point;
}
// Max minimum points in
// subarray of size k
System.out.println(final_point);
}
// Driver Code
public static void main (String[] args)
{
int arr[] = { 2, 1, 4, 2, 3, 4, 1, 2 };
int n = arr.length;
int k = 4;
minpoint(arr, n, k);
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation to find the
# maximum number of valley elements
# in the subarrays of size K
# Function to find the valley elements
# in the array which contains
# in the subarrays of the size K
def minpoint(arr, n, k):
min_point = 0
for i in range(1, k-1):
# Increment min_point
# if element at index i
# is smaller than element
# at index i + 1 and i-1
if(arr[i] < arr[i - 1] and arr[i] < arr[i + 1]):
min_point += 1
# final_point to maintain maximum
# of min points of subarray
final_point = min_point
# Iterate over array
# from kth element
for i in range(k, n):
# Leftmost element of subarray
if(arr[i - ( k - 1 )] < arr[i - ( k - 1 ) + 1] and\
arr[i - ( k - 1 )] < arr[i - ( k - 1 ) - 1]):
min_point -= 1
# Rightmost element of subarray
if(arr[i - 1] < arr[i] and arr[i - 1] < arr[i - 2]):
min_point += 1
# if new subarray have greater
# number of min points than previous
# subarray, then final_point is modified
if(min_point > final_point):
final_point = min_point
# Max minimum points in
# subarray of size k
print(final_point)
# Driver Code
if __name__ == "__main__":
arr = [2, 1, 4, 2, 3, 4, 1, 2]
n = len(arr)
k = 4
minpoint(arr, n, k)
C#
// C# implementation to find the
// maximum number of valley elements
// in the subarrays of size K
using System;
class GFG{
// Function to find the valley elements
// in the array which contains
// in the subarrays of the size K
static void minpoint(int []arr, int n, int k)
{
int min_point = 0;
for(int i = 1; i < k - 1; i++)
{
// Increment min_point
// if element at index i
// is smaller than element
// at index i + 1 and i-1
if(arr[i] < arr[i - 1] &&
arr[i] < arr[i + 1])
min_point += 1;
}
// final_point to maintain maximum
// of min points of subarray
int final_point = min_point;
// Iterate over array
// from kth element
for(int i = k ; i < n; i++)
{
// Leftmost element of subarray
if(arr[i - ( k - 1 )] < arr[i - ( k - 1 ) + 1] &&
arr[i - ( k - 1 )] < arr[i - ( k - 1 ) - 1])
min_point -= 1;
// Rightmost element of subarray
if(arr[i - 1] < arr[i] &&
arr[i - 1] < arr[i - 2])
min_point += 1;
// If new subarray have greater
// number of min points than previous
// subarray, then final_point is modified
if(min_point > final_point)
final_point = min_point;
}
// Max minimum points in
// subarray of size k
Console.WriteLine(final_point);
}
// Driver Code
public static void Main (string[] args)
{
int []arr = { 2, 1, 4, 2, 3, 4, 1, 2 };
int n = arr.Length;
int k = 4;
minpoint(arr, n, k);
}
}
// This code is contributed by AnkitRai01
Javascript
输出:
1
时间复杂度: O(N)