给定N个正整数的数组arr [] 。我们可以选择数组的任何一个索引(例如K ),并将数组的所有元素从索引0减少到K – 1减1。此操作的成本为K。如果任何index(say 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]范围内的最小元素,再次执行上述操作。
- 重复上述步骤,直到数组的第一个元素不为零。
- 上述步骤中的操作数是最大的操作成本。
时间复杂度: 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)