给定由N个整数组成的数组arr [] ,任务是找到需要添加的最小正值S ,以使加S后给定数组的每个索引处的前缀和始终为正。
例子:
Input: arr[] = {-3, 2, -3, 4, 2}
Output: 5
Explanation:
For S = 5, prefix sums at each array indices are as follows:
At index 1: (5 – 3) = 2
At index 2: (2 + 2) = 4
At index 3: (4 – 3) = 1
At index 4: (1 + 4) = 5
At index 5: (5 + 2) = 7
Since all the prefix sums obtained is greater than 0, the required answer is 5.
Input: arr[] = {1, 2}
Output: 1
天真的方法:最简单的方法是对从1开始的S的所有可能值进行迭代,并将S添加到每个数组索引的前缀和上,并检查它是否大于0 。打印所有前缀和都为正的S值。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效方法:要优化上述方法,请按照以下步骤操作:
- 用0初始化变量minValue以存储最小前缀和。
- 初始化变量和以在每个索引处存储前缀和。
- 使用变量i在[0,N – 1]范围内遍历数组arr [] 。
- 更新总和并将arr [i]添加到总和。
- 如果sum小于minValue ,则将minValue设置为sum 。
- 初始化变量startValue以存储所需的结果,并将startValue设置为等于(1 – minValue) 。
- 完成上述步骤后,将startValue的值打印为S的最小可能值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find minimum startValue
// for positive prefix sum at each index
int minStartValue(vector& nums)
{
// Store the minimum prefix sum
int minValue = 0;
// Stores prefix sum at each index
int sum = 0;
// Traverse over the array
for (auto n : nums) {
// Update the prefix sum
sum += n;
// Update the minValue
minValue = min(minValue, sum);
}
int startValue = 1 - minValue;
// Return the positive start value
return startValue;
}
// Driver Code
int main()
{
vector nums = { -3, 2, -3, 4, 2 };
// Function Call
cout << minStartValue(nums);
return 0;
}
Java
// Java program for the
// above approach
import java.util.*;
class GFG{
// Function to find minimum startValue
// for positive prefix sum at each index
static int minStartValue(int[] nums)
{
// Store the minimum prefix sum
int minValue = 0;
// Stores prefix sum at each index
int sum = 0;
// Traverse over the array
for(int n : nums)
{
// Update the prefix sum
sum += n;
// Update the minValue
minValue = Math.min(minValue, sum);
}
int startValue = 1 - minValue;
// Return the positive start value
return startValue;
}
// Driver Code
public static void main(String[] args)
{
int[] nums = { -3, 2, -3, 4, 2 };
// Function Call
System.out.println(minStartValue(nums));
}
}
// This code is contributed by susmitakundugoaldanga
Python3
# Python3 program for the
# above approach
# Function to find minimum startValue
# for positive prefix sum at each index
def minStartValue(nums):
# Store the minimum prefix sum
minValue = 0
# Stores prefix sum at each index
sum = 0
# Traverse over the array
for i in range(len(nums)):
# Update the prefix sum
sum += nums[i]
# Update the minValue
minValue = min(minValue, sum)
startValue = 1 - minValue
# Return the positive start value
return startValue
# Driver Code
if __name__ == '__main__':
nums = [ -3, 2, -3, 4, 2 ]
# Function Call
print(minStartValue(nums))
# This code is contributed by Princi Singh
C#
// C# program for the above approach
using System;
class GFG{
// Function to find minimum startValue
// for positive prefix sum at each index
static int minStartValue(int[] nums)
{
// Store the minimum prefix sum
int minValue = 0;
// Stores prefix sum at each index
int sum = 0;
// Traverse over the array
foreach(int n in nums)
{
// Update the prefix sum
sum += n;
// Update the minValue
minValue = Math.Min(minValue, sum);
}
int startValue = 1 - minValue;
// Return the positive start value
return startValue;
}
// Driver Code
public static void Main()
{
int[] nums = { -3, 2, -3, 4, 2 };
// Function Call
Console.WriteLine(minStartValue(nums));
}
}
// This code is contributed by code_hunt
Javascript
输出:
5
时间复杂度: O(N)
辅助空间: O(1)