📌  相关文章
📜  最多替换一次后,最小化给定阵列中的峰和谷数

📅  最后修改于: 2022-05-13 01:56:06.501000             🧑  作者: Mango

最多替换一次后,最小化给定阵列中的峰和谷数

给定一个包含N个正整数的数组arr[] ,任务是通过最多替换具有任何值的给定数组。

注意:数组的第一个和最后一个元素不能是峰或谷,因为它们没有两个邻居。

例子:

方法:给定的问题可以使用贪心方法来解决。请注意,第 i 个索引的任何更改仅影响第(i+1) 个和第(i-1) 个索引处的元素。请按照以下步骤解决此问题:

  • 将布尔数组标记初始化为 false。
  • 此外,将一个整数变量total初始化为 0。它跟踪给定序列的整体平衡。
  • 现在迭代给定的数组,如果数组的第i元素是波峰波谷,则将其第 i索引标记为真。
  • 将另一个变量ans初始化为total 。它跟踪最终答案。
  • 现在再次遍历数组,
    • 假设,当前迭代在索引i处,
      • 使索引i处的元素等于i + 1并立即更新数组的总余额。
      • 使索引i处的元素等于i – 1并立即更新数组的总余额。
      • 通过创建函数isUp()isDown()可以轻松实现上述情况的总平衡,分别检查特定索引处是否存在峰值和谷值。
      • 此外,相应地更新ans变量。
  • 打印ans变量表示的值。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check up at the index i
bool isUp(int i, int arr[], int N)
{
    return (i > 0 && i < N - 1
            && arr[i] < arr[i - 1]
            && arr[i] < arr[i + 1]);
}
 
// Function to check down at the index i
bool isDown(int i, int arr[], int N)
{
    return (i > 0 && i < N - 1
            && arr[i] > arr[i - 1]
            && arr[i] > arr[i + 1]);
}
 
// Solve function
int solve(int arr[], int N)
{
    // Initializing a boolean array
    bool mark[N] = { 0 };
 
    int total = 0;
 
    // Iterate over the array
    for (int i = 1; i < N - 1; i++) {
 
        // Peak
        if (arr[i] > arr[i + 1]
            && arr[i] > arr[i - 1]) {
            mark[i] = 1;
            total++;
        }
 
        // Trough
        else if (arr[i] < arr[i + 1]
                 && arr[i] < arr[i - 1]) {
            mark[i] = 1;
            total++;
        }
    }
 
    // Initialize ans variable as total
    int ans = total;
    for (int i = 1; i < N - 1; i++) {
 
        // Make arr[i] equal to arr[i - 1]
        int temp = arr[i];
        arr[i] = arr[i - 1];
        ans
 
            = min(
                ans,
                total - mark[i - 1] - mark[i]
                    - mark[i + 1]
                    + isUp(i - 1, arr, N)
                    + isDown(i - 1, arr, N)
                    + isUp(i, arr, N)
                    + isDown(i, arr, N)
                    + isUp(i + 1, arr, N)
                    + isDown(i + 1, arr, N));
 
        // Make arr[i] equal to arr[i + 1]
        arr[i] = arr[i + 1];
        ans
            = min(
                ans,
                total
                    - mark[i - 1] - mark[i]
                    - mark[i + 1]
                    + isUp(i - 1, arr, N)
                    + isDown(i - 1, arr, N)
                    + isUp(i, arr, N)
                    + isDown(i, arr, N)
                    + isUp(i + 1, arr, N)
                    + isDown(i + 1, arr, N));
        arr[i] = temp;
    }
 
    // Return the ans
    return ans;
}
 
// Menu driver code
int main()
{
    // Initializing an array
    int arr[] = { 1, 4, 3, 5, 3, 8 };
 
    // Size of arr
    int N = sizeof(arr) / sizeof(int);
 
    // Calling solve function
    cout << solve(arr, N);
 
    return 0;
}


Java
// Java code for the above approach
import java.util.*;
 
class GFG{
 
  // Function to check up at the index i
  static int isUp(int i, int arr[], int N)
  {
    if (i > 0 && i < N - 1
        && arr[i] < arr[i - 1]
        && arr[i] < arr[i + 1])
    {
      return 1;
    }
    else
      return 0;
  }
 
  // Function to check down at the index i
  static int isDown(int i, int arr[], int N)
  {
    if (i > 0 && i < N - 1
        && arr[i] > arr[i - 1]
        && arr[i] > arr[i + 1])
    {
      return 1;
    }
    else
      return 0;
  }
 
  // Solve function
  static int solve(int arr[], int N)
  {
    // Initializing a boolean array
    int mark[] = new int[N] ;
 
 
    int total = 0;
 
    // Iterate over the array
    for (int i = 1; i < N - 1; i++) {
 
      // Peak
      if (arr[i] > arr[i + 1]
          && arr[i] > arr[i - 1]) {
        mark[i] = 1;
        total++;
      }
 
      // Trough
      else if (arr[i] < arr[i + 1]
               && arr[i] < arr[i - 1]) {
        mark[i] = 1;
        total++;
      }
    }
 
    // Initialize ans variable as total
    int ans = total;
    for (int i = 1; i < N - 1; i++) {
 
      // Make arr[i] equal to arr[i - 1]
      int temp = arr[i];
      arr[i] = arr[i - 1];
      ans
 
        = Math.min(
        ans,
        total - mark[i - 1] - mark[i]
        - mark[i + 1]
        + isUp(i - 1, arr, N)
        + isDown(i - 1, arr, N)
        + isUp(i, arr, N)
        + isDown(i, arr, N)
        + isUp(i + 1, arr, N)
        + isDown(i + 1, arr, N));
 
      // Make arr[i] equal to arr[i + 1]
      arr[i] = arr[i + 1];
      ans
        = Math.min(
        ans,
        total
        - mark[i - 1] - mark[i]
        - mark[i + 1]
        + isUp(i - 1, arr, N)
        + isDown(i - 1, arr, N)
        + isUp(i, arr, N)
        + isDown(i, arr, N)
        + isUp(i + 1, arr, N)
        + isDown(i + 1, arr, N));
      arr[i] = temp;
    }
 
    // Return the ans
    return ans;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
 
    // Initializing an array
    int arr[] = { 1, 4, 3, 5, 3, 8 };
 
    // Size of arr
    int N = arr.length;
 
    // Calling solve function
    System.out.print(solve(arr, N));
  }
}
 
// This code is contributed by sanjoy_62.


Python3
# Pytohn program for the above approach
 
# Function to check up at the index i
def isUp (i, arr, N):
    return (i > 0 and i < N - 1
        and arr[i] < arr[i - 1]
        and arr[i] < arr[i + 1]);
 
# Function to check down at the index i
def isDown (i, arr, N):
    return (i > 0 and i < N - 1
        and arr[i] > arr[i - 1]
        and arr[i] > arr[i + 1]);
 
# Solve function
def solve (arr, N):
    # Initializing a boolean array
    mark = [0] * N
 
    total = 0;
 
    # Iterate over the array
    for i in range(1, N - 1):
 
        # Peak
        if (arr[i] > arr[i + 1] and arr[i] > arr[i - 1]):
            mark[i] = 1;
            total += 1
         
        # Trough
        elif (arr[i] < arr[i + 1]
            and arr[i] < arr[i - 1]):
            mark[i] = 1;
            total += 1
 
    # Initialize ans variable as total
    ans = total;
    for i in range(1, N - 1):
 
        # Make arr[i] equal to arr[i - 1]
        temp = arr[i];
        arr[i] = arr[i - 1];
        ans = min(
            ans,
            total - mark[i - 1] - mark[i]
            - mark[i + 1]
            + isUp(i - 1, arr, N)
            + isDown(i - 1, arr, N)
            + isUp(i, arr, N)
            + isDown(i, arr, N)
            + isUp(i + 1, arr, N)
            + isDown(i + 1, arr, N));
 
        # Make arr[i] equal to arr[i + 1]
        arr[i] = arr[i + 1];
        ans = min(
            ans,
            total
            - mark[i - 1] - mark[i]
            - mark[i + 1]
            + isUp(i - 1, arr, N)
            + isDown(i - 1, arr, N)
            + isUp(i, arr, N)
            + isDown(i, arr, N)
            + isUp(i + 1, arr, N)
            + isDown(i + 1, arr, N));
        arr[i] = temp;
     
    # Return the ans
    return ans;
 
# Menu driver code
 
# Initializing an array
arr = [1, 4, 3, 5, 3, 8];
 
# Size of arr
N = len(arr)
 
# Calling solve function
print(solve(arr, N));
 
# This code is contributed by gfgking


C#
// C# code for the above approach
using System;
 
class GFG {
 
  // Function to check up at the index i
  static int isUp(int i, int []arr, int N)
  {
    if (i > 0 && i < N - 1
        && arr[i] < arr[i - 1]
        && arr[i] < arr[i + 1])
    {
      return 1;
    }
    else
      return 0;
  }
 
  // Function to check down at the index i
  static int isDown(int i, int []arr, int N)
  {
    if (i > 0 && i < N - 1
        && arr[i] > arr[i - 1]
        && arr[i] > arr[i + 1])
    {
      return 1;
    }
    else
      return 0;
  }
 
  // Solve function
  static int solve(int []arr, int N)
  {
    // Initializing a boolean array
    int []mark = new int[N] ;
 
 
    int total = 0;
 
    // Iterate over the array
    for (int i = 1; i < N - 1; i++) {
 
      // Peak
      if (arr[i] > arr[i + 1]
          && arr[i] > arr[i - 1]) {
        mark[i] = 1;
        total++;
      }
 
      // Trough
      else if (arr[i] < arr[i + 1]
               && arr[i] < arr[i - 1]) {
        mark[i] = 1;
        total++;
      }
    }
 
    // Initialize ans variable as total
    int ans = total;
    for (int i = 1; i < N - 1; i++) {
 
      // Make arr[i] equal to arr[i - 1]
      int temp = arr[i];
      arr[i] = arr[i - 1];
      ans
 
        = Math.Min(
        ans,
        total - mark[i - 1] - mark[i]
        - mark[i + 1]
        + isUp(i - 1, arr, N)
        + isDown(i - 1, arr, N)
        + isUp(i, arr, N)
        + isDown(i, arr, N)
        + isUp(i + 1, arr, N)
        + isDown(i + 1, arr, N));
 
      // Make arr[i] equal to arr[i + 1]
      arr[i] = arr[i + 1];
      ans
        = Math.Min(
        ans,
        total
        - mark[i - 1] - mark[i]
        - mark[i + 1]
        + isUp(i - 1, arr, N)
        + isDown(i - 1, arr, N)
        + isUp(i, arr, N)
        + isDown(i, arr, N)
        + isUp(i + 1, arr, N)
        + isDown(i + 1, arr, N));
      arr[i] = temp;
    }
 
    // Return the ans
    return ans;
  }
 
  // Driver Code
  public static void Main()
  {
 
    // Initializing an array
    int []arr = { 1, 4, 3, 5, 3, 8 };
 
    // Size of arr
    int N = arr.Length;
 
    // Calling solve function
    Console.Write(solve(arr, N));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
1

时间复杂度: 在)
辅助空间: 在)