给定大小为N的数组arr [] ,任务是找到可分配给每个数组元素arr [i]的正整数的最小和,以使如果arr [i]> arr [i + 1]或arr [i – 1] ,则分配给arr [i]的正整数必须超过分配给arr [i + 1]或arr [i – 1]的整数。
例子:
Input: arr[] = {1, 0, 2}
Output: 5
Explanation: One possible way to assign positive integers is ans[] = {2, 1, 2} such that the following conditions are satisfied:
- arr[0] > arr[1] and ans[0] > ans[1]
- arr[1] < arr[2] and ans[1] < ans[2]
Therefore, minimum possible sum = 2 + 1 + 2 = 5.
Input: arr[] = {1, 2, 2}
Output: 4
Explanation: One possible way to assign positive integers is ans[] = {1, 2, 1}. Therefore, the minimum possible sum = 1 + 2 + 1 = 4.
方法:这个想法是从左到右,从右到左遍历给定的数组,以更新每个元素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
4
时间复杂度: O(N)
辅助空间: O(N)