📌  相关文章
📜  求最大总和等于三层的总和

📅  最后修改于: 2021-05-07 00:39:48             🧑  作者: Mango

给定三个正数的堆栈,任务是找到堆栈中可能相等的最大和,并允许除去顶部元素。堆栈表示为一个数组,而数组的第一个索引表示堆栈的顶部元素。

例子:

Input : stack1[] = { 3, 10}
  stack2[] = { 4, 5 }
  stack3[] = { 2, 1 }
Output : 0
Sum can only be equal after removing all elements 
from all stacks.

查找三个堆栈的最大和相等

想法是比较每个堆栈的总和,如果它们不相同,则删除堆栈中具有最大总和的顶部元素。
解决此问题的算法:

  1. 查找各个堆栈中的所有元素的总和。
  2. 如果所有三个堆栈的总和相同,则这是最大总和。
  3. 否则,删除三个堆栈中具有最大总和的堆栈的顶部元素。重复步骤1和步骤2。

该方法之所以有效,是因为要素是积极的。为了使总和相等,我们必须从堆栈中删除一些具有更多总和的元素,并且只能从顶部移除。

以下是此方法的实现:

C++
// C++ program to calculate maximum sum with equal
// stack sum.
#include 
using namespace std;
 
// Returns maximum possible equal sum of three stacks
// with removal of top elements allowed
int maxSum(int stack1[], int stack2[], int stack3[], int n1,
           int n2, int n3)
{
    int sum1 = 0, sum2 = 0, sum3 = 0;
 
    // Finding the initial sum of stack1.
    for (int i = 0; i < n1; i++)
        sum1 += stack1[i];
 
    // Finding the initial sum of stack2.
    for (int i = 0; i < n2; i++)
        sum2 += stack2[i];
 
    // Finding the initial sum of stack3.
    for (int i = 0; i < n3; i++)
        sum3 += stack3[i];
 
    // As given in question, first element is top
    // of stack..
    int top1 = 0, top2 = 0, top3 = 0;
    while (1) {
        // If any stack is empty
        if (top1 == n1 || top2 == n2 || top3 == n3)
            return 0;
 
        // If sum of all three stack are equal.
        if (sum1 == sum2 && sum2 == sum3)
            return sum1;
 
        // Finding the stack with maximum sum and
        // removing its top element.
        if (sum1 >= sum2 && sum1 >= sum3)
            sum1 -= stack1[top1++];
        else if (sum2 >= sum1 && sum2 >= sum3)
            sum2 -= stack2[top2++];
        else if (sum3 >= sum2 && sum3 >= sum1)
            sum3 -= stack3[top3++];
    }
}
 
// Driven Program
int main()
{
    int stack1[] = { 3, 2, 1, 1, 1 };
    int stack2[] = { 4, 3, 2 };
    int stack3[] = { 1, 1, 4, 1 };
 
    int n1 = sizeof(stack1) / sizeof(stack1[0]);
    int n2 = sizeof(stack2) / sizeof(stack2[0]);
    int n3 = sizeof(stack3) / sizeof(stack3[0]);
 
    cout << maxSum(stack1, stack2, stack3, n1, n2, n3)
         << endl;
    return 0;
}


Java
// JAVA Code for Find maximum sum possible
// equal sum of three stacks
class GFG {
      
    // Returns maximum possible equal sum of three
    // stacks with removal of top elements allowed
    public static int maxSum(int stack1[], int stack2[],
                            int stack3[], int n1, int n2,
                                               int n3)
    {
      int sum1 = 0, sum2 = 0, sum3 = 0;
       
      // Finding the initial sum of stack1.
      for (int i=0; i < n1; i++)
          sum1 += stack1[i];
      
      // Finding the initial sum of stack2.
      for (int i=0; i < n2; i++)
          sum2 += stack2[i];
      
      // Finding the initial sum of stack3.
      for (int i=0; i < n3; i++)
          sum3 += stack3[i];
      
      // As given in question, first element is top
      // of stack..
      int top1 =0, top2 = 0, top3 = 0;
      int ans = 0;
      while (true)
      {
          // If any stack is empty
          if (top1 == n1 || top2 == n2 || top3 == n3)
             return 0;
      
          // If sum of all three stack are equal.
          if (sum1 == sum2 && sum2 == sum3)
             return sum1;
          
          // Finding the stack with maximum sum and
          // removing its top element.
          if (sum1 >= sum2 && sum1 >= sum3)
             sum1 -= stack1[top1++];
          else if (sum2 >= sum1 && sum2 >= sum3)
             sum2 -= stack2[top2++];
          else if (sum3 >= sum2 && sum3 >= sum1)
             sum3 -= stack3[top3++];
       }
    }
     
    /* Driver program to test above function */
    public static void main(String[] args)
    {
          int stack1[] = { 3, 2, 1, 1, 1 };
          int stack2[] = { 4, 3, 2 };
          int stack3[] = { 1, 1, 4, 1 };
          
          int n1 = stack1.length;
          int n2 = stack2.length;
          int n3 = stack3.length;
          
          System.out.println(maxSum(stack1, stack2,
                               stack3, n1, n2, n3));
    }
  }
// This code is contributed by Arnav Kr. Mandal.


Python
# Python program to calculate maximum sum with equal
# stack sum.
# Returns maximum possible equal sum of three stacks
# with removal of top elements allowed
def maxSum(stack1, stack2, stack3, n1, n2, n3):
    sum1, sum2, sum3 = 0, 0, 0
   
  # Finding the initial sum of stack1.
    for i in range(n1):
        sum1 += stack1[i]
  
  # Finding the initial sum of stack2.
    for i in range(n2):
        sum2 += stack2[i]
  
  # Finding the initial sum of stack3.
    for i in range(n3):
        sum3 += stack3[i]
  
  # As given in question, first element is top
  # of stack..
    top1, top2, top3 = 0, 0, 0
    ans = 0
    while (1):
      # If any stack is empty
        if (top1 == n1 or top2 == n2 or top3 == n3):
            return 0
  
      # If sum of all three stack are equal.
        if (sum1 == sum2 and sum2 == sum3):
            return sum1
      
      # Finding the stack with maximum sum and
      # removing its top element.
        if (sum1 >= sum2 and sum1 >= sum3):
            sum1 -= stack1[top1]
            top1=top1+1
        elif (sum2 >= sum1 and sum2 >= sum3):
            sum2 -= stack2[top2]
            top2=top2+1
        elif (sum3 >= sum2 and sum3 >= sum1):
            sum3 -= stack3[top3]
            top3=top3+1
  
# Driven Program
stack1 = [ 3, 2, 1, 1, 1 ]
stack2 = [ 4, 3, 2 ]
stack3 = [ 1, 1, 4, 1 ]
  
n1 = len(stack1)
n2 = len(stack2)
n3 = len(stack3)
  
print maxSum(stack1, stack2, stack3, n1, n2, n3)
 
#This code is contributed by Afzal Ansari


C#
// C# Code for Find maximum sum with
// equal sum of three stacks
using System;
 
class GFG {
 
    // Returns maximum possible equal
    // sum of three stacks with removal
    // of top elements allowed
    public static int maxSum(int[] stack1,
               int[] stack2, int[] stack3,
                   int n1, int n2, int n3)
    {
         
        int sum1 = 0, sum2 = 0, sum3 = 0;
 
        // Finding the initial sum of
        // stack1.
        for (int i = 0; i < n1; i++)
            sum1 += stack1[i];
 
        // Finding the initial sum of
        // stack2.
        for (int i = 0; i < n2; i++)
            sum2 += stack2[i];
 
        // Finding the initial sum of
        // stack3.
        for (int i = 0; i < n3; i++)
            sum3 += stack3[i];
 
        // As given in question, first
        // element is top of stack..
        int top1 = 0, top2 = 0, top3 = 0;
 
        while (true) {
             
            // If any stack is empty
            if (top1 == n1 || top2 == n2
                            || top3 == n3)
                return 0;
 
            // If sum of all three stack
            // are equal.
            if (sum1 == sum2 && sum2 == sum3)
                return sum1;
 
            // Finding the stack with maximum
            // sum and removing its top element.
            if (sum1 >= sum2 && sum1 >= sum3)
                sum1 -= stack1[top1++];
            else if (sum2 >= sum1 && sum2 >= sum3)
                sum2 -= stack2[top2++];
            else if (sum3 >= sum2 && sum3 >= sum1)
                sum3 -= stack3[top3++];
        }
    }
 
    /* Driver program to test above function */
    public static void Main()
    {
        int[] stack1 = { 3, 2, 1, 1, 1 };
        int[] stack2 = { 4, 3, 2 };
        int[] stack3 = { 1, 1, 4, 1 };
 
        int n1 = stack1.Length;
        int n2 = stack2.Length;
        int n3 = stack3.Length;
 
        Console.Write(maxSum(stack1, stack2,
                        stack3, n1, n2, n3));
    }
}
 
// This code is contributed by nitin mittal.


PHP
= $sum2 && $sum1 >= $sum3)
                $sum1 -= $stack1[$top1++];
                 
        else if ($sum2 >= $sum1 && $sum2 >=$sum3)
                $sum2 -= $stack2[$top2++];
                 
        else if ($sum3 >= $sum2 && $sum3 >= $sum1)
                $sum3 -= $stack3[$top3++];
    }
}
 
// Driver Code
$stack1 = array(3, 2, 1, 1, 1);
$stack2 = array(4, 3, 2);
$stack3 = array(1, 1, 4, 1);
 
$n1 = sizeof($stack1);
$n2 = sizeof($stack2);
$n3 = sizeof($stack3);
echo maxSum($stack1, $stack2,
            $stack3, $n1,
            $n2, $n3) ;
             
// This code is contributed by nitin mittal
?>


Javascript


输出
5

时间复杂度: O(n1 + n2 + n3)其中n1,n2和n3是三个堆栈的大小。

https://youtu.be/PZ

-TNgKVX-0?list = PLqM7alHXFySF7Lap-wi5qlaD8OEBx9RMV