根据给定的容量条件可以得到的数组元素的最大可能值
给定两个数组arr[]和cap[]都由N个正整数组成,使得第i个元素cap[i]表示arr[i]的容量,任务是找到数组元素的最大可能值,可以是如果相邻元素的最终值不超过其相应的容量,则允许将数组元素arr[i]减少某个任意值,并将其任何相邻元素增加相同的值。
例子:
Input: arr[] = {2, 3}, cap[] = {5, 6}
Output: 5
Explanation:
Following operations are performed to maximize value of any element in arr[]:
Operation 1: Decrease arr[0] by 2 and increase arr[1] by 2. Now arr[] = {0, 5}.
Therefore, the maximum element in arr[] is 5.
Input: arr[] = {1, 2, 1}, cap[] = {2, 3, 2}
Output: 3
方法:给定的问题可以通过使用贪心方法来解决,该方法基于观察到在执行任意数量的操作后最大值不能超过 cap[] 中的最大容量。因此答案将是min(所有数组元素的总和 , cap[] 中的最大容量) 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum element
// after shifting operations in arr[]
int maxShiftArrayValue(int arr[], int cap[],
int N)
{
// Stores the sum of array element
int sumVals = 0;
for (int i = 0; i < N; i++) {
sumVals += arr[i];
}
// Stores the maximum element in cap[]
int maxCapacity = 0;
// Iterate to find maximum element
for (int i = 0; i < N; i++) {
maxCapacity = max(cap[i], maxCapacity);
}
// Return the resultant maximum value
return min(maxCapacity, sumVals);
}
// Driver Code
int main()
{
int arr[] = { 2, 3 };
int cap[] = { 5, 6 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << maxShiftArrayValue(arr, cap, N);
return 0;
}
Java
// Java program for the above approach
class GFG
{
// Function to find the maximum element
// after shifting operations in arr[]
public static int maxShiftArrayValue(int arr[], int cap[], int N)
{
// Stores the sum of array element
int sumVals = 0;
for (int i = 0; i < N; i++) {
sumVals += arr[i];
}
// Stores the maximum element in cap[]
int maxCapacity = 0;
// Iterate to find maximum element
for (int i = 0; i < N; i++) {
maxCapacity = Math.max(cap[i], maxCapacity);
}
// Return the resultant maximum value
return Math.min(maxCapacity, sumVals);
}
// Driver Code
public static void main(String args[]) {
int arr[] = { 2, 3 };
int cap[] = { 5, 6 };
int N = arr.length;
System.out.println(maxShiftArrayValue(arr, cap, N));
}
}
// This code is contributed by gfgking.
Python3
# Python 3 program for the above approach
# Function to find the maximum element
# after shifting operations in arr[]
def maxShiftArrayValue(arr, cap, N):
# Stores the sum of array element
sumVals = 0
for i in range(N):
sumVals += arr[i]
# Stores the maximum element in cap[]
maxCapacity = 0
# Iterate to find maximum element
for i in range(N):
maxCapacity = max(cap[i], maxCapacity)
# Return the resultant maximum value
return min(maxCapacity, sumVals)
# Driver Code
if __name__ == '__main__':
arr = [2, 3]
cap = [5, 6]
N = len(arr)
print(maxShiftArrayValue(arr, cap, N))
# This code is contributed by ipg2016107.
C#
// C# program for the above approach
using System;
class GFG {
// Function to find the maximum element
// after shifting operations in arr[]
public static int maxShiftArrayValue(int[] arr,
int[] cap, int N)
{
// Stores the sum of array element
int sumVals = 0;
for (int i = 0; i < N; i++) {
sumVals += arr[i];
}
// Stores the maximum element in cap[]
int maxCapacity = 0;
// Iterate to find maximum element
for (int i = 0; i < N; i++) {
maxCapacity = Math.Max(cap[i], maxCapacity);
}
// Return the resultant maximum value
return Math.Min(maxCapacity, sumVals);
}
// Driver Code
public static void Main(string[] args)
{
int[] arr = { 2, 3 };
int[] cap = { 5, 6 };
int N = arr.Length;
Console.WriteLine(maxShiftArrayValue(arr, cap, N));
}
}
// This code is contributed by ukasp.
Javascript
输出:
5
时间复杂度: O(N)
辅助空间: O(N)