给定数组arr [] ,任务是选择大小为K的子数组,该子数组包含相对于相邻元素的最大谷点数。
如果一个元素arr [i]的两个相邻元素都大于该谷点,则称为谷点,即和 。
例子:
Input: arr[] = {5, 4, 6, 4, 5, 2, 3, 1}, K = 7
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 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
输出:
1
时间复杂度: O(N)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。