给定一个由N 个正整数组成的数组arr[] 。我们可以选择数组的任何一个索引(比如K ),并将数组的所有元素从索引0 减少到K – 1减 1。这个操作的成本是K 。如果在任何索引处(比如idx )元素减少到0,那么我们不能在[idx, N]范围内执行此操作。任务是最大化减少操作的成本,直到我们无法执行操作。
例子:
Input: arr[] = {3, 2, 1, 4, 5}
Output: 8
Explanation:
Firstly, choose k=5 then remove each element from 0 to 4, and array becomes [2, 1, 0, 3, 4].
Secondly, choose k=2 then remove each element from 0 to 1, and array becomes [1, 0, 0, 3, 4].
Finally, choose k=1, then remove each element from 0 to 0, and array becomes [0, 0, 0, 3, 4].
Hence, the answer is 5+2+1=8
Input: arr[] = {4, 3, 2, 1}
Output: 10
Explanation:
Choose k=4 then 3 then 2 then 1, hence answer becomes 4+3+2+1=10
天真的方法:
- 从[0, N]范围内的数组中选择最小元素。
- 让最小元素位于索引idx 处。
- 将所有元素减少 1 idx次数并计算此操作。
- 现在索引idx处的元素为 0。通过选择[0, idx]范围内的最小元素再次执行上述操作。
- 重复上述步骤,直到数组的第一个元素不为零。
- 上述步骤中的操作次数为最大17m的操作成本。
时间复杂度: O(N 2 )
辅助空间: O(1)
有效的方法:为了最大化操作的成本,总是从数组中选择最大的索引。在选择最大索引(例如idx )并执行操作时,如果在任何索引处元素减少为零,则将选择索引减少到idx 。以下是步骤:
- 遍历给定数组。
- 在遍历数组时找到迄今为止找到的最小元素,直到每个索引。
- 每个索引的最小元素的总和是所需操作的总成本。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function that finds the maximum cost
// of all the operations
int maxCost(int arr[], int N)
{
// Initialise maxi with positive
// integer value
int maxi = INT_MAX;
// Initialise the answer variable
int ans = 0;
// Iterate linearly in the array
for(int i = 0; i < N; i++)
{
// Find minimum at each step
maxi = min(maxi, arr[i]);
// Add maximum to ans
ans = ans + maxi;
}
// Return the answer
return ans;
}
// Driver code
int main()
{
// Length of the array
int N = 4;
// Given array arr[]
int arr[] = { 4, 3, 2, 1 };
// Function call
int answer = maxCost(arr, N);
// Print the result
cout << (answer);
}
// This code is contributed by princiraj1992
Java
// Java program for the above approach
class GFG{
// Function that finds the maximum cost
// of all the operations
public static int maxCost(int arr[], int N)
{
// Initialise maxi with positive
// integer value
int maxi = Integer.MAX_VALUE;
// Initialise the answer variable
int ans = 0;
// Iterate linearly in the array
for(int i = 0; i < N; i++)
{
// Find minimum at each step
maxi = Math.min(maxi, arr[i]);
// Add maximum to ans
ans = ans + maxi;
}
// Return the answer
return ans;
}
// Driver code
public static void main(String[] args)
{
// Length of the array
int N = 4;
// Given array arr[]
int arr[] = { 4, 3, 2, 1 };
// Function call
int answer = maxCost(arr, N);
// Print the result
System.out.println(answer);
}
}
// This code is contributed by stutipathak31jan
Python3
# Python3 program for the above approach
# Function that finds the maximum cost
# of all the operations
def maxCost(arr, N):
# Initialize maxi with positive
# infinity value
maxi = float("inf")
# Initialise the answer variable
ans = 0
# Iterate linearly in the array
for i in range(N):
# Find minimum at each step
maxi = min(maxi, arr[i])
# Add maximum to ans
ans = ans + maxi
# Return the answer
return ans
# Driver Code
if __name__ == '__main__':
# Length of the array
N = 4
# Given array arr[]
arr = [4, 3, 2, 1]
# Function call
answer = maxCost(arr, N)
# Print the result
print(answer)
C#
// C# program for the above approach
using System;
class GFG{
// Function that finds the maximum cost
// of all the operations
public static int maxCost(int []arr, int N)
{
// Initialise maxi with positive
// integer value
int maxi = int.MaxValue;
// Initialise the answer variable
int ans = 0;
// Iterate linearly in the array
for(int i = 0; i < N; i++)
{
// Find minimum at each step
maxi = Math.Min(maxi, arr[i]);
// Add maximum to ans
ans = ans + maxi;
}
// Return the answer
return ans;
}
// Driver code
public static void Main(String[] args)
{
// Length of the array
int N = 4;
// Given array []arr
int []arr = { 4, 3, 2, 1 };
// Function call
int answer = maxCost(arr, N);
// Print the result
Console.WriteLine(answer);
}
}
// This code is contributed by Rajput-Ji
10
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live