📌  相关文章
📜  圆形数组中的最大和,以使没有两个元素相邻

📅  最后修改于: 2021-04-29 01:58:11             🧑  作者: Mango

给定一个包含正整数值的圆形数组。任务是找到一个子序列的最大和,其约束是该序列中的任何2个数字都不应在数组中相邻。

例子:

Input: circular arr = {1, 2, 3, 1}
Output : 4
subsequence will be(1, 3), hence 1 + 3 = 4 

Input: circular arr = {1, 2, 3, 4, 5, 1}
Output: 9
subsequence will be(1, 3, 5), hence 1 + 3 + 5 = 9 

方法使用DP可以解决问题。这篇文章中已经讨论了一种方法,但是它适用于数组。我们可以将圆形子数组分为两个数组,分别是从(0th到n-2-th)和(1st到n-1-th)索引,并使用上一篇文章中使用的方法。两者返回的最大和就是答案。

下面是上述方法的实现。

C++
// CPP program to find maximum sum in a circular array 
// such that no elements are adjacent in the sum.
#include 
using namespace std;
  
// Function to calculate the sum
// from 0th position to(n-2)th position
int maxSum1(int arr[], int n)
{
    int dp[n];
    int maxi = 0;
  
    for (int i = 0; i < n - 1; i++) {
  
        // copy the element of original array to dp[]
        dp[i] = arr[i];
  
        // find the maximum element in the array
        if (maxi < arr[i])
            maxi = arr[i];
    }
  
    // start from 2nd to n-1th pos
    for (int i = 2; i < n - 1; i++) {
  
        // traverse for all pairs
        // bottom-up approach
        for (int j = 0; j < i - 1; j++) {
  
            // dp-condition
            if (dp[i] < dp[j] + arr[i]) {
                dp[i] = dp[j] + arr[i];
  
                // find maximum sum
                if (maxi < dp[i])
                    maxi = dp[i];
            }
        }
    }
  
    // return the maximum
    return maxi;
}
  
// Function to find the maximum sum
// from 1st position to n-1-th position
int maxSum2(int arr[], int n)
{
    int dp[n];
    int maxi = 0;
  
    for (int i = 1; i < n; i++) {
        dp[i] = arr[i];
  
        if (maxi < arr[i])
            maxi = arr[i];
    }
  
    // Traverse from third to n-th pos
    for (int i = 3; i < n; i++) {
  
        // bootom-up approach
        for (int j = 1; j < i - 1; j++) {
  
            // dp condition
            if (dp[i] < arr[i] + dp[j]) {
                dp[i] = arr[i] + dp[j];
  
                // find max sum
                if (maxi < dp[i])
                    maxi = dp[i];
            }
        }
    }
  
    // return max
    return maxi;
}
  
int findMaxSum(int arr[], int n)
{
   return max(maxSum1(arr, n), maxSum2(arr, n));
}
  
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 1 };
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << findMaxSum(arr, n);
    return 0;
}


Java
// Java  program to find maximum sum in a circular array 
// such that no elements are adjacent in the sum. 
  
import java.io.*;
  
class GFG {
      
      
// Function to calculate the sum 
// from 0th position to(n-2)th position 
static int maxSum1(int arr[], int n) 
{ 
    int dp[]=new int[n];
    int maxi = 0; 
  
    for (int i = 0; i < n - 1; i++) { 
  
        // copy the element of original array to dp[] 
        dp[i] = arr[i]; 
  
        // find the maximum element in the array 
        if (maxi < arr[i]) 
            maxi = arr[i]; 
    } 
  
    // start from 2nd to n-1th pos 
    for (int i = 2; i < n - 1; i++) { 
  
        // traverse for all pairs 
        // bottom-up approach 
        for (int j = 0; j < i - 1; j++) { 
  
            // dp-condition 
            if (dp[i] < dp[j] + arr[i]) { 
                dp[i] = dp[j] + arr[i]; 
  
                // find maximum sum 
                if (maxi < dp[i]) 
                    maxi = dp[i]; 
            } 
        } 
    } 
  
    // return the maximum 
    return maxi; 
} 
  
// Function to find the maximum sum 
// from 1st position to n-1-th position 
static int maxSum2(int arr[], int n) 
{ 
    int dp[]=new int[n];
    int maxi = 0; 
  
    for (int i = 1; i < n; i++) { 
        dp[i] = arr[i]; 
  
        if (maxi < arr[i]) 
            maxi = arr[i]; 
    } 
  
    // Traverse from third to n-th pos 
    for (int i = 3; i < n; i++) { 
  
        // bootom-up approach 
        for (int j = 1; j < i - 1; j++) { 
  
            // dp condition 
            if (dp[i] < arr[i] + dp[j]) { 
                dp[i] = arr[i] + dp[j]; 
  
                // find max sum 
                if (maxi < dp[i]) 
                    maxi = dp[i]; 
            } 
        } 
    } 
  
    // return max 
    return maxi; 
} 
  
static int findMaxSum(int arr[], int n) 
{ 
    int t=Math.max(maxSum1(arr, n), maxSum2(arr, n));
    return  t;
} 
  
// Driver Code 
    public static void main (String[] args) {
          
        int arr[] = { 1, 2, 3, 1 }; 
        int n = arr.length; 
        System.out.println(findMaxSum(arr, n)); 
          
    }
}


Python 3
# Python 3 program to find maximum sum 
# in a circular array such that no 
# elements are adjacent in the sum.
  
# Function to calculate the sum from
# 0th position to(n-2)th position
def maxSum1(arr, n):
  
    dp = [0] * n
    maxi = 0
  
    for i in range(n - 1):
  
        # copy the element of original 
        # array to dp[]
        dp[i] = arr[i]
  
        # find the maximum element in the array
        if (maxi < arr[i]):
            maxi = arr[i]
  
    # start from 2nd to n-1th pos
    for i in range(2, n - 1):
  
        # traverse for all pairs bottom-up
        # approach
        for j in range(i - 1) :
  
            # dp-condition
            if (dp[i] < dp[j] + arr[i]):
                dp[i] = dp[j] + arr[i]
  
                # find maximum sum
                if (maxi < dp[i]):
                    maxi = dp[i]
  
    # return the maximum
    return maxi
  
# Function to find the maximum sum
# from 1st position to n-1-th position
def maxSum2(arr, n):
  
    dp = [0] * n
    maxi = 0
  
    for i in range(1, n):
        dp[i] = arr[i]
  
        if (maxi < arr[i]):
            maxi = arr[i]
  
    # Traverse from third to n-th pos
    for i in range(3, n):
  
        # bootom-up approach
        for j in range(1, i - 1) :
  
            # dp condition
            if (dp[i] < arr[i] + dp[j]):
                dp[i] = arr[i] + dp[j]
  
                # find max sum
                if (maxi < dp[i]):
                    maxi = dp[i]
  
    # return max
    return maxi
  
def findMaxSum(arr, n):
    return max(maxSum1(arr, n), maxSum2(arr, n))
  
# Driver Code
if __name__ == "__main__":
      
    arr = [ 1, 2, 3, 1 ]
    n = len(arr)
    print(findMaxSum(arr, n))
  
# This code is contributed by ita_c


C#
// C# program to find maximum sum
// in a circular array such that 
// no elements are adjacent in the sum. 
using System;
  
class GFG
{
// Function to calculate the sum 
// from 0th position to(n-2)th position 
static int maxSum1(int []arr, int n) 
{ 
    int []dp = new int[n]; 
    int maxi = 0; 
  
    for (int i = 0; i < n - 1; i++)
    { 
  
        // copy the element of original
        // array to dp[] 
        dp[i] = arr[i]; 
  
        // find the maximum element 
        // in the array 
        if (maxi < arr[i]) 
            maxi = arr[i]; 
    } 
  
    // start from 2nd to n-1th pos 
    for (int i = 2; i < n - 1; i++) 
    { 
  
        // traverse for all pairs 
        // bottom-up approach 
        for (int j = 0; j < i - 1; j++) 
        { 
  
            // dp-condition 
            if (dp[i] < dp[j] + arr[i])
            { 
                dp[i] = dp[j] + arr[i]; 
  
                // find maximum sum 
                if (maxi < dp[i]) 
                    maxi = dp[i]; 
            } 
        } 
    } 
  
    // return the maximum 
    return maxi; 
} 
  
// Function to find the maximum sum 
// from 1st position to n-1-th position 
static int maxSum2(int []arr, int n) 
{ 
    int []dp = new int[n]; 
    int maxi = 0; 
  
    for (int i = 1; i < n; i++) 
    { 
        dp[i] = arr[i]; 
  
        if (maxi < arr[i]) 
            maxi = arr[i]; 
    } 
  
    // Traverse from third to n-th pos 
    for (int i = 3; i < n; i++)
    { 
  
        // bootom-up approach 
        for (int j = 1; j < i - 1; j++)
        { 
  
            // dp condition 
            if (dp[i] < arr[i] + dp[j])
            { 
                dp[i] = arr[i] + dp[j]; 
  
                // find max sum 
                if (maxi < dp[i]) 
                    maxi = dp[i]; 
            } 
        } 
    } 
  
    // return max 
    return maxi; 
} 
  
static int findMaxSum(int []arr, int n) 
{ 
    int t = Math.Max(maxSum1(arr, n), 
                     maxSum2(arr, n)); 
    return t; 
} 
  
// Driver Code 
static public void Main ()
{
    int []arr = { 1, 2, 3, 1 }; 
    int n = arr.Length; 
    Console.WriteLine(findMaxSum(arr, n)); 
}
}
  
// This code is contributed
// by Sach_Code


PHP


输出:
4

时间复杂度: O(N ^ 2)