给定一个由 N 个正数组成的数组 A[],任务是找到可以形成的最大和,其中不存在三个连续元素。
例子:
Input: A[] = {1, 2, 3}, N=3
Output: 5
Explanation: Three of them can’t be taken together so answer is 2 + 3 = 5
Input: A[] = {3000, 2000, 1000, 3, 10}, N=5
Output: 5013
此处讨论了采用O(N)辅助空间的O(N)方法。这可以通过以下需要O(1)额外空间的方法进一步优化。
O(1) 空间方法:从上面的方法,我们可以得出结论,对于计算sum[i],只有sum[i-1]、sum[i-2]和sum[i-3] 的值是相关的。这种观察有助于完全丢弃 sum 数组,而只保留一些变量来使用 O(1) 辅助空间解决问题。
请按照以下步骤解决问题:
- 初始化要使用的以下变量:
- sum:这会存储最终的总和,这样没有三个元素是连续的。
- first:这将子序列总和存储到索引i-1 。
- 第二个:这将子序列总和存储到索引i-2 。
- 第三:这存储了索引i-3的子序列总和。
- 如果N<3 ,则答案将是所有元素的总和,因为不会有连续元素。
- 否则,请执行以下操作:
- 用A[0]初始化第三个
- 用A[0]+A[1]初始化第二个
- 首先用max(second, A[1]+A[2])初始化
- 使用first 、 second和third 中的最大值初始化sum 。
- 从3迭代到N-1 ,并对每个当前索引i执行以下操作:
- 可能有以下三种情况:
- 排除A[i],即sum = first
- 排除A[i-1],即sum = second + A[i]
- 排除A[i-2] ,即sum = third + A[i] + A[i-1]
- 因此, sum更新为first 、 (second+A[i])和(third+A[i]+A[i-1])之间的最大值
- 更新第三,第二,第二,第一和第一与总和。
- 可能有以下三种情况:
- 最后,返回sum 。
下面是上述方法的实现:
C++
// C++ implementation for the above approach
#include
using namespace std;
// Function to calculate the maximum subsequence sum such
// that no three elements are consecutive
int maxSumWO3Consec(int A[], int N)
{
// when N is 1, answer would be the only element present
if (N == 1)
return A[0];
// when N is 2, answer would be sum of elements
if (N == 2)
return A[0] + A[1];
// variable to store sum up to i - 3
int third = A[0];
// variable to store sum up to i - 2
int second = third + A[1];
// variable to store sum up to i - 1
int first = max(second, A[1] + A[2]);
// variable to store the final sum of the subsequence
int sum = max(max(third, second), first);
for (int i = 3; i < N; i++) {
// find the maximum subsequence sum up to index i
sum = max(max(first, second + A[i]),
third + A[i] + A[i - 1]);
// update first, second and third
third = second;
second = first;
first = sum;
}
// return ans;
return sum;
}
// Driver code
int main()
{
// Input
int A[] = { 3000, 2000, 1000, 3, 10 };
int N = sizeof(A) / sizeof(A[0]);
// Function call
cout << maxSumWO3Consec(A, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG
{
// Function to calculate the maximum subsequence sum
// such
// that no three elements are consecutive
public static int maxSumWO3Consec(int A[], int N)
{
// when N is 1, answer would be the only element
// present
if (N == 1)
return A[0];
// when N is 2, answer would be sum of elements
if (N == 2)
return A[0] + A[1];
// variable to store sum up to i - 3
int third = A[0];
// variable to store sum up to i - 2
int second = third + A[1];
// variable to store sum up to i - 1
int first = Math.max(second, A[1] + A[2]);
// variable to store the final sum of the
// subsequence
int sum = Math.max(Math.max(third, second), first);
for (int i = 3; i < N; i++)
{
// find the maximum subsequence sum up to index
// i
sum = Math.max(Math.max(first, second + A[i]),
third + A[i] + A[i - 1]);
// update first, second and third
third = second;
second = first;
first = sum;
}
// return ans;
return sum;
}
public static void main(String[] args)
{
// Input
int A[] = { 3000, 2000, 1000, 3, 10 };
int N = A.length;
// Function call
int res = maxSumWO3Consec(A, N);
System.out.println(res);
}
}
//This code is contributed by Potta Lokesh
Python3
# Python 3 implementation for the above approach
# Function to calculate the maximum subsequence sum such
# that no three elements are consecutive
def maxSumWO3Consec(A, N):
# when N is 1, answer would be the only element present
if (N == 1):
return A[0]
# when N is 2, answer would be sum of elements
if (N == 2):
return A[0] + A[1]
# variable to store sum up to i - 3
third = A[0]
# variable to store sum up to i - 2
second = third + A[1]
# variable to store sum up to i - 1
first = max(second, A[1] + A[2])
# variable to store the final sum of the subsequence
sum = max(max(third, second), first)
for i in range(3,N,1):
# find the maximum subsequence sum up to index i
sum = max(max(first, second + A[i]), third + A[i] + A[i - 1])
# update first, second and third
third = second
second = first
first = sum
# return ans;
return sum
# Driver code
if __name__ == '__main__':
# Input
A = [3000, 2000, 1000, 3, 10]
N = len(A)
# Function call
print(maxSumWO3Consec(A, N))
# This code is contributed by ipg2016107.
C#
// C# program for the above approach
using System;
class GFG {
// Function to calculate the maximum subsequence sum
// such
// that no three elements are consecutive
public static int maxSumWO3Consec(int[] A, int N)
{
// when N is 1, answer would be the only element
// present
if (N == 1)
return A[0];
// when N is 2, answer would be sum of elements
if (N == 2)
return A[0] + A[1];
// variable to store sum up to i - 3
int third = A[0];
// variable to store sum up to i - 2
int second = third + A[1];
// variable to store sum up to i - 1
int first = Math.Max(second, A[1] + A[2]);
// variable to store the final sum of the
// subsequence
int sum = Math.Max(Math.Max(third, second), first);
for (int i = 3; i < N; i++) {
// find the maximum subsequence sum up to index
// i
sum = Math.Max(Math.Max(first, second + A[i]),
third + A[i] + A[i - 1]);
// update first, second and third
third = second;
second = first;
first = sum;
}
// return ans;
return sum;
}
// Driver Code
public static void Main()
{
// Input
int[] A = { 3000, 2000, 1000, 3, 10 };
int N = A.Length;
// Function call
int res = maxSumWO3Consec(A, N);
Console.Write(res);
}
}
Javascript
输出:
5013
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。