📌  相关文章
📜  找到最大和可能等于三个堆栈的总和

📅  最后修改于: 2021-10-26 05:31:37             🧑  作者: 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。

该方法有效,因为元素是积极的。为了使 sum 相等,我们必须从堆栈中删除一些具有更多 sum 的元素,我们只能从顶部删除。

下面是这个方法的实现:

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程