📌  相关文章
📜  通过按该顺序为每个递增/递减的连续对分配值来实现最小总和

📅  最后修改于: 2021-09-04 08:37:47             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是找到可以分配给每个数组元素arr[i]的最小正整数总和,如果arr[i] > arr[i+1]arr [i – 1] ,那么分配给arr[i]的正整数必须超过分配给arr[i + 1]arr[i – 1]的整数

例子:

方法:想法是从左到右,从右到左遍历给定的数组,更新每个元素arr[i]的答案,使得arr[i]的答案大于arr[i+1的答案]arr[i – 1]如果arr[i]大于arr[i + 1]arr[i – 1] 。请按照以下步骤解决问题:

  • 初始化一个大小为N的向量ans ,它存储可以分配给每个元素的最小正整数。
  • 1初始化向量ans ,因为必须为每个元素分配一些正整数。
  • 使用变量i从左到右遍历数组,如果arr[i]大于arr[i – 1]则将ans[i]更新为ans[i] = ans[i – 1] + 1
  • 现在,如果arr[i]大于arr[i+1] ,则使用变量i从右到左遍历数组,然后将ans[i]更新为ans[i] = max(ans[i], ans[i + 1] + 1)
  • 现在,找到向量ans 中存在的正整数的总和,并将其打印为可能的最小总和。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to print the minimum sum
// of values assigned to each element
// of the array as per given conditions
void minSum(int* arr, int n)
{
    // Initialize vectors with value 1
    vector ans(n, 1);
 
    // Traverse from left to right
    for (int i = 1; i < n; i++) {
 
        // Update if ans[i] > ans[i-1]
        if (arr[i] > arr[i - 1]) {
            ans[i] = max(ans[i],
                         ans[i - 1] + 1);
        }
    }
 
    // Traverse from right to left
    for (int i = n - 2; i >= 0; i--) {
 
        // Update as ans[i] > ans[i+1]
        // if arr[i]> arr[i+1]
        if (arr[i] > arr[i + 1]) {
            ans[i] = max(ans[i],
                         ans[i + 1] + 1);
        }
    }
 
    // Find the minimum sum
    int s = 0;
    for (auto x : ans) {
        s = s + x;
    }
 
    // Print the sum
    cout << s << endl;
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 1, 2, 2 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    minSum(arr, N);
 
    return 0;
}


Java
// Java program for the
// above approach
import java.util.*;
class GFG{
 
// Function to print the
// minimum sum of values
// assigned to each element
// of the array as per given
// conditions
static void minSum(int[] arr,
                   int n)
{
  // Initialize vectors
  // with value 1
  int[] ans = new int[n];
  Arrays.fill(ans, 1);
 
  // Traverse from left
  // to right
  for (int i = 1; i < n; i++)
  {
    // Update if ans[i] > ans[i-1]
    if (arr[i] > arr[i - 1])
    {
      ans[i] = Math.max(ans[i],
                        ans[i - 1] + 1);
    }
  }
 
  // Traverse from right to left
  for (int i = n - 2; i >= 0; i--)
  {
    // Update as ans[i] > ans[i+1]
    // if arr[i]> arr[i+1]
    if (arr[i] > arr[i + 1])
    {
      ans[i] = Math.max(ans[i],
                        ans[i + 1] + 1);
    }
  }
 
  // Find the minimum sum
  int s = 0;
  for (int x : ans)
  {
    s = s + x;
  }
 
  // Print the sum
  System.out.print(s + "\n");
}
 
// Driver Code
public static void main(String[] args)
{
  // Given array arr[]
  int arr[] = {1, 2, 2};
 
  int N = arr.length;
 
  // Function Call
  minSum(arr, N);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the
# above approach
 
# Function to print the minimum
# sum of values assigned to each
# element of the array as per
# given conditions
def minSum(arr, n):
 
    # Initialize vectors with
    # value 1
    ans = [1] * (n)
 
    # Traverse from left to
    # right
    for i in range(1, n):
 
        # Update if ans[i] >
        # ans[i-1]
        if (arr[i] > arr[i - 1]):
            ans[i] = max(ans[i],
                         ans[i - 1] + 1)
 
    # Traverse from right
    # to left
    for i in range(n - 2,
                   -1, -1):
 
        # Update as ans[i] >
        # ans[i+1] if arr[i] >
        # arr[i+1]
        if (arr[i] > arr[i + 1]):
            ans[i] = max(ans[i],
                         ans[i + 1] + 1)
 
    # Find the minimum sum
    s = 0
    for x in ans:
        s = s + x
 
    # Print the sum
    print(s)
 
# Driver Code
if __name__ == "__main__":
 
    # Given array arr[]
    arr = [1, 2, 2]
 
    N = len(arr)
 
    # Function Call
    minSum(arr, N)
 
# This code is contributed by Chitranayal


C#
// C# program for the
// above approach
using System;
class GFG{
 
// Function to print the
// minimum sum of values
// assigned to each element
// of the array as per given
// conditions
static void minSum(int[] arr,
                   int n)
{
  // Initialize vectors
  // with value 1
  int[] ans = new int[n];
   
  for(int i = 0; i < n; i++)
    ans[i] = 1;
 
  // Traverse from left
  // to right
  for (int i = 1; i < n; i++)
  {
    // Update if ans[i] > ans[i-1]
    if (arr[i] > arr[i - 1])
    {
      ans[i] = Math.Max(ans[i],
                        ans[i - 1] + 1);
    }
  }
 
  // Traverse from right to left
  for (int i = n - 2; i >= 0; i--)
  {
    // Update as ans[i] > ans[i+1]
    // if arr[i]> arr[i+1]
    if (arr[i] > arr[i + 1])
    {
      ans[i] = Math.Max(ans[i],
                        ans[i + 1] + 1);
    }
  }
 
  // Find the minimum sum
  int s = 0;
  foreach (int x in ans)
  {
    s = s + x;
  }
 
  // Print the sum
  Console.Write(s + "\n");
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given array []arr
  int []arr = {1, 2, 2};
 
  int N = arr.Length;
 
  // Function Call
  minSum(arr, N);
}
}
 
// This code is contributed by gauravrajput1


Javascript


输出
4

时间复杂度: O(N)
辅助空间: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live