求严格递增子数组的最大和
给定一个正整数数组。求严格递增子数组的最大和。请注意,此问题不同于最大子数组和和最大和增加子序列问题。
例子:
Input : arr[] = {1, 2, 3, 2, 5, 1, 7}
Output : 8
Explanation : Some Strictly increasing subarrays are
{1, 2, 3} sum = 6,
{2, 5} sum = 7,
{1, 7} sum 8
Maximum Sum = 8
Input : arr[] = {1, 2, 2, 4}
Output: 6
Explanation : Increasing subarray with maximum sum is 6.
一个简单的解决方案是生成所有可能的子数组,并为每个子数组检查子数组是否严格增加。如果子数组是严格递增的,那么我们计算 sum 并更新 max_sum。时间复杂度 O(n 2 )。
这个问题的有效解决方案需要 O(n) 时间。这个想法是跟踪最大总和和当前总和。对于每个元素 arr[i],如果它大于 arr[i-1],那么我们将它添加到当前总和中。否则 arr[i] 是最大和增加子数组的另一个潜在候选者的起点,因此我们将当前和更新为数组。但是在更新当前总和之前,如果需要,我们会更新最大总和。
Let input array be 'arr[]' and size of array be 'n'
Initialize :
max_sum = arr[0]// because if array size is 1 than it would return that element.
// used to store the maximum sum
current_sum = arr[0] // used to compute current sum
// Traverse array starting from second element
i goes from 1 to n-1
// Check if it is strictly increasing then we
// update current_sum.
current_sum = current_sum + arr[i]
max_sum = max(max_sum, current_sum)// Also needed for subarray having last element.
// else strictly increasing subarray breaks and
// arr[i] is starting point of next potential
// subarray
max_sum = max(max_sum, current_sum)
current_sum = arr[i]
return max(max_sum, current_sum)
下面是上述思想的实现。
C++
// C/C++ program to find the maximum sum of strictly
// increasing subarrays
#include
using namespace std;
// Returns maximum sum of strictly increasing
// subarrays
int maxsum_SIS(int arr[], int n)
{
// Initialize max_sum be 0
int max_sum = arr[0];
// Initialize current sum be arr[0]
int current_sum = arr[0];
// Traverse array elements after first element.
for (int i = 1; i < n; i++)
{
// update current_sum for
// strictly increasing subarray
if (arr[i - 1] < arr[i])
{
current_sum = current_sum + arr[i];
max_sum = max(max_sum, current_sum);
}
else // strictly increasing subarray break
{
// update max_sum and current_sum ;
max_sum = max(max_sum, current_sum);
current_sum = arr[i];
}
}
return max(max_sum, current_sum);
}
// Driver code
int main()
{
int arr[] = { 1, 2, 2, 4 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Maximum sum : " << maxsum_SIS(arr, n);
return 0;
}
Java
// Java program to find the
// maximum sum of strictly increasing subarrays
public class GFG {
// Returns maximum sum
// of strictly increasing subarrays
static int maxsum_SIS(int arr[], int n)
{
// Initialize max_sum be 0
int max_sum = arr[0];
// Initialize current sum be arr[0]
int current_sum = arr[0];
// Traverse array elements after first element.
for (int i = 1; i < n; i++)
{
// update current_sum
// for strictly increasing subarray
if (arr[i - 1] < arr[i])
{
current_sum = current_sum + arr[i];
max_sum = Math.max(max_sum, current_sum);
}
else // strictly increasing subarray break
{
// update max_sum and current_sum ;
max_sum = Math.max(max_sum, current_sum);
current_sum = arr[i];
}
}
return Math.max(max_sum, current_sum);
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 2, 2, 4 };
int n = arr.length;
System.out.println("Maximum sum : "
+ maxsum_SIS(arr, n));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to find the maximum sum of strictly
# increasing subarrays
# Returns maximum sum of strictly increasing
# subarrays
def maxsum_SIS(arr, n):
# Initialize max_sum be 0
max_sum = arr[0]
# Initialize current sum be arr[0]
current_sum = arr[0]
# Traverse array elements after first element.
for i in range(1, n):
# update current_sum for strictly increasing subarray
if (arr[i-1] < arr[i]):
current_sum = current_sum + arr[i]
max_sum = max(max_sum, current_sum)
else:
# strictly increasing subarray break
# update max_sum and current_sum
max_sum = max(max_sum, current_sum)
current_sum = arr[i]
return max(max_sum, current_sum)
# Driver code
def main():
arr = [1, 2, 2, 4]
n = len(arr)
print("Maximum sum : ", maxsum_SIS(arr, n)),
if __name__ == '__main__':
main()
# This code is contributed by 29AjayKumar
C#
// C# program to find the maximum sum of strictly
// increasing subarrays
using System;
public class GFG {
// Returns maximum sum of strictly increasing
// subarrays
static int maxsum_SIS(int[] arr, int n)
{
// Initialize max_sum be 0
int max_sum = arr[0];
// Initialize current sum be arr[0]
int current_sum = arr[0];
// Traverse array elements after first element.
for (int i = 1; i < n; i++) {
// update current_sum for strictly increasing
// subarray
if (arr[i - 1] < arr[i]) {
current_sum = current_sum + arr[i];
max_sum = Math.Max(max_sum, current_sum);
}
else // strictly increasing subarray break
{
// update max_sum and current_sum ;
max_sum = Math.Max(max_sum, current_sum);
current_sum = arr[i];
}
}
return Math.Max(max_sum, current_sum);
}
// Driver code
public static void Main()
{
int[] arr = { 1, 2, 2, 4 };
int n = arr.Length;
Console.WriteLine("Maximum sum : "
+ maxsum_SIS(arr, n));
}
}
// This code is contributed by 29AjayKumar
PHP
Javascript
输出
Maximum sum : 6
时间复杂度: O(n)
辅助空间: O(1)