使 Array 最多为 0 的最小递减量,使得所有数组元素在一个数字减少到 0 后循环递减
给定一个包含N个整数的循环数组arr[]和一个数组cost[] ,任务是计算使数组的所有元素都等于0所需的最小操作数,其中在每个操作中递减索引i的值由1 。 如果索引的值变为 0,则将arr[i+1]的值减少cost[i] ,将arr[i + 2]的值减少cost[i + 1]等等。
注意:如果一个元素变得小于 0,则认为它是 0。
例子:
Input: arr[] = {7, 2, 5}, cost[] = {8, 9, 3}
Output: 6
Explanation: Decrements can be made in the following way:
- Decrement the value of arr[1] twice. Hence, the value of arr[2] will be decremented by cost[1] and the value of arr[0] will be decremented by cost[2]. Therefore, the final array will be arr[] = {4, 0, 0}.
- Now, decrement the value of arr[0], 4 times to make it 0. Therefore, the array becomes arr[] = {0, 0, 0}.
Hence, the number of required operations to make all the elements of the array equal to zero is 6 which is the minimum possible.
Input: arr[] = {6, 7, 7, 10, 8, 2}, cost[] = {5, 10, 1, 4, 7, 7}
Output: 16
方法:给定的问题可以通过以下观察使用贪心方法来解决:
- 数组arr[]的最后一个剩余非零元素,假设x将需要x减量操作。
- 假设在减量操作后, arr[i]的值变为0 ,那么对于arr[i] ,所需的减量数为arr[i] ,对于arr[i+1] ,所需的减量数为arr [i+1] – max(0, arr[i+1] – cost[i])等等。
以下是要遵循的步骤:
- 使用变量i遍历[0, N)范围内的给定数组arr[] 。
- 使数组的第 i个索引等于 0 所需的操作数是arr[i] – min(arr[i], cost[i-1]) 。因此,在变量ans中的所有索引上保持该值的总和。
- 在[0, N)范围内的所有索引上,将ans的值增加 arr[i] 或 cost[i] 的最小值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find minimum decrements
// required to make all array elements 0
int minDecrements(int arr[], int powr[], int N)
{
// Variable to store the answer
int ans = 0;
int mn = INT_MAX;
// Traverse the array
for (int i = 0; i < N; i++) {
int idx = (i + 1) % N;
int val = min(arr[idx], powr[i]);
ans += arr[idx] - val;
// Store the minimum one
mn = min(mn, val);
}
ans += mn;
// Return the ans
return ans;
}
// Driver Code
int main()
{
int arr[] = { 6, 7, 7, 10, 8, 2 };
int powr[] = { 5, 10, 1, 4, 7, 7 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << minDecrements(arr, powr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class GFG
{
// Function to find minimum decrements
// required to make all array elements 0
static int minDecrements(int []arr, int []powr, int N)
{
// Variable to store the answer
int ans = 0;
int mn = Integer.MAX_VALUE;
// Traverse the array
for (int i = 0; i < N; i++) {
int idx = (i + 1) % N;
int val = Math.min(arr[idx], powr[i]);
ans += arr[idx] - val;
// Store the minimum one
mn = Math.min(mn, val);
}
ans += mn;
// Return the ans
return ans;
}
// Driver Code
public static void main(String args[])
{
int []arr = { 6, 7, 7, 10, 8, 2 };
int []powr = { 5, 10, 1, 4, 7, 7 };
int N = arr.length;
System.out.println(minDecrements(arr, powr, N));
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python program for the above approach
# Function to find minimum decrements
# required to make all array elements 0
def minDecrements(arr, powr, N):
# Variable to store the answer
ans = 0
mn = 99999999
# Traverse the array
for i in range(N):
idx = (i + 1) % N
val = min(arr[idx], powr[i])
ans += arr[idx] - val
# Store the minimum one
mn = min(mn, val)
ans += mn
# Return the ans
return ans
# Driver Code
if __name__ == "__main__":
arr = [6, 7, 7, 10, 8, 2]
powr = [5, 10, 1, 4, 7, 7]
N = len(arr)
print(minDecrements(arr, powr, N))
# This code is contributed by Potta Lokesh
C#
// C# program for the above approach
using System;
using System.Collections;
class GFG
{
// Function to find minimum decrements
// required to make all array elements 0
static int minDecrements(int []arr, int []powr, int N)
{
// Variable to store the answer
int ans = 0;
int mn = Int32.MaxValue;
// Traverse the array
for (int i = 0; i < N; i++) {
int idx = (i + 1) % N;
int val = Math.Min(arr[idx], powr[i]);
ans += arr[idx] - val;
// Store the minimum one
mn = Math.Min(mn, val);
}
ans += mn;
// Return the ans
return ans;
}
// Driver Code
public static void Main()
{
int []arr = { 6, 7, 7, 10, 8, 2 };
int []powr = { 5, 10, 1, 4, 7, 7 };
int N = arr.Length;
Console.Write(minDecrements(arr, powr, N));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
16
时间复杂度: O(N)
辅助空间: O(1)