给定数组中的最小替换以删除所有严格的峰值元素
给定一个长度为N的数组arr[] ,任务是找到移除数组中所有峰值元素所需的最小替换次数。
注意:如果一个元素严格大于其两个相邻元素,则称该元素为峰值元素。角元素不能是峰元素,因为它们只有一个邻居。
例子:
Input: arr = { 3, 2, 3}
Output: operations = 0
arr = { 3, 2, 3}
Explanation : There is no peak element in array
Input: arr = { 2, 2, 3, 1, 3, 1 3, 1, 3}
Output: operations = 2
arr = {2, 2, 3, 3, 3, 1, 3, 3, 3 }
Explanation: There are three peak elements at arr[2], arr[4] and arr[6].
Replace arr[3] with arr[4] and arr[7] with arr[8]
方法:解决这个问题的想法是将谷元素转换为严格相邻的峰值元素。这可以如下所示完成:
If an element arr[i] is a peak element, replace arr[i +1] with max of arr[i+2] and arr[ i ]. This will eliminate the chances of arr[ i ] and arr[i+2] of becoming peak elements at the same time.
If i+2 exceeds the array bound then replace arr[i] with max of arr[i-1] and arr[i+1] as this will eliminate chance of arr[i] being a peak.
请按照以下步骤解决问题:
- 从数组的开头迭代。
- 检查第 i 个元素是否为峰值:
- 如果是峰值,则将替换计数增加1并根据上述条件更改。
- 否则,跳过此元素并继续迭代。
- 返回最终计数和最终数组。
下面是上述方法的实现。
C++
// C++ code to implement the approach
#include
using namespace std;
// Function to find the count
// of replacements and the final array
void solution(int* arr, int n)
{
// Count of operations
int cnt = 0;
for (int i = 1; i < n - 1; i++) {
// Check if it is peak or not
if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) {
cnt++;
if (i < n - 2)
arr[i + 1] = max(arr[i], arr[i + 2]);
else
arr[i] = max(arr[i + 1], arr[i - 1]);
}
}
cout << (cnt) << "\n";
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
}
// Driver Code
int main()
{
int N = 9;
int arr[] = { 2, 2, 3, 1, 3, 1, 3, 1, 3 };
solution(arr, N);
return 0;
}
// This code is contributed by rakeshsahni
Java
// Java code to implement the approach
import java.io.*;
class GFG {
// Function to find the count
// of replacements and the final array
public static void solution(int[] arr,
int n)
{
// Count of operations
int cnt = 0;
for (int i = 1; i < n - 1; i++) {
// Check if it is peak or not
if (arr[i] > arr[i - 1]
&& arr[i] > arr[i + 1]) {
cnt++;
if (i < n - 2)
arr[i + 1]
= Math.max(arr[i],
arr[i + 2]);
else
arr[i]
= Math.max(arr[i + 1],
arr[i - 1]);
}
}
System.out.println(cnt);
for (int i : arr)
System.out.print(i + " ");
}
// Driver Code
public static void main(String[] args)
{
int N = 9;
int arr[] = { 2, 2, 3, 1, 3, 1, 3, 1, 3 };
solution(arr, N);
}
}
Python
# Python code to implement the approach
# Function to find the count
# of replacements and the final array
def solution(arr, n):
# Count of operations
cnt = 0
i = 1
for i in range(1, n - 1):
# Check if it is peak or not
if (arr[i] > arr[i - 1] and arr[i] > arr[i + 1]):
cnt += 1
if (i < n - 2):
arr[i + 1] = max(arr[i], arr[i + 2])
else:
arr[i] = max(arr[i + 1], arr[i - 1])
print(cnt)
print(arr)
# Driver Code
N = 9
arr = [ 2, 2, 3, 1, 3, 1, 3, 1, 3 ]
solution(arr, N)
# This code is contributed by Samim Hossain Mondal.
C#
// C# code to implement the approach
using System;
class GFG {
// Function to find the count
// of replacements and the final array
public static void solution(int[] arr,
int n)
{
// Count of operations
int cnt = 0;
for (int i = 1; i < n - 1; i++) {
// Check if it is peak or not
if (arr[i] > arr[i - 1]
&& arr[i] > arr[i + 1]) {
cnt++;
if (i < n - 2)
arr[i + 1]
= Math.Max(arr[i],
arr[i + 2]);
else
arr[i]
= Math.Max(arr[i + 1],
arr[i - 1]);
}
}
Console.WriteLine(cnt);
foreach (int i in arr)
Console.Write(i + " ");
}
// Driver Code
public static void Main()
{
int N = 9;
int []arr = { 2, 2, 3, 1, 3, 1, 3, 1, 3 };
solution(arr, N);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
2
2 2 3 3 3 1 3 3 3
时间复杂度: O(N)
辅助空间: O(1)