📜  数组值的三角形排列的最大高度

📅  最后修改于: 2021-04-29 17:10:00             🧑  作者: Mango

给定一个数组,我们需要从数组值中找到可以形成的三角形的最大高度,以使每个(i + 1)级别包含更多元素,而上一个级别的总和更大。
例子:

Input : a = { 40, 100, 20, 30 }
Output : 2
Explanation : We can have 100 and 20 at the bottom level and either 40 or 30 at the upper level of the pyramid

Input : a = { 20, 20, 10, 10, 5, 2 }
Output : 3

首先,乍一看,我们可能不得不看一下数组值。但事实并非如此。这是此问题的棘手部分。在这里,我们不必关心数组的值,因为我们可以将数组的任何元素排列在满足这些条件的三角形值中。即使所有元素都一样,例如array = {3 ,, 3,3,3,3},我们也可以有解决方案。我们可以在底部放置两个3,在顶部放置一个3,或在底部放置三个3,在顶部放置两个3。您可以使用自己的任何示例,并且总会找到在配置中进行排列的解决方案。因此,如果最大高度为2,那么我们至少应在底部拥有2个元素,在顶部至少具有1个元素,这意味着我们应至少具有3个元素(2 *(2 + 1)/ 2)。同样,对于3作为高度,我们在数组中应至少包含6个元素。
因此,我们的最终解决方案仅在于以下逻辑:如果我们的金字塔具有最大可能的高度h ,则(h *(h + 1))/ 2个元素必须存在于数组中。
下面是上述方法的实现:

C++
// C++ program to find the maximum height
// of Pyramidal Arrangement of array values
#include 
using namespace std;
 
int MaximumHeight(int a[], int n)
{
    int result = 1;
    for (int i = 1; i <= n; ++i) {
 
        // Just checking whether ith level
        // is possible or not if possible
        // then we must have atleast
        // (i*(i+1))/2 elements in the
        // array
        long long y = (i * (i + 1)) / 2;
 
        // updating the result value
        // each time
        if (y < n)
            result = i;
         
        // otherwise we have exceeded n value
        else
            break;
    }
    return result;
}
 
int main()
{
    int arr[] = { 40, 100, 20, 30 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << MaximumHeight(arr, n);
    return 0;
}


Java
// Java program to find 
// the maximum height of
// Pyramidal Arrangement
// of array values
import java.io.*;
 
class GFG
{
static int MaximumHeight(int []a,
                         int n)
{
         
    int result = 1;
    for (int i = 1; i <= n; ++i)
    {
 
        // Just checking whether
        // ith level is possible
        // or not if possible then
        // we must have atleast
        // (i*(i+1))/2 elements
        // in the array
        int y = (i * (i + 1)) / 2;
 
        // updating the result
        // value each time
        if (y < n)
            result = i;
         
        // otherwise we have
        // exceeded n value
        else
            break;
    }
    return result;
}
 
// Driver Code
public static void main (String[] args)
{
    int []arr = { 40, 100, 20, 30 };
    int n = arr.length;
    System.out.println(MaximumHeight(arr, n));
}
}
 
// This code is contributed by ajit


Python3
# Python program to find the
# maximum height of Pyramidal
# Arrangement of array values
 
def MaximumHeight(a, n):
    result = 1
 
    for i in range(1, n):
         
        # Just checking whether ith level
        # is possible or not if possible
        # then we must have atleast
        # (i*(i+1))/2 elements in the array
        y = (i * (i + 1)) / 2
 
        # updating the result
        # value each time
        if(y < n):
            result = i
             
        # otherwise we have
        # exceeded n value
        else:
            break
 
    return result
 
# Driver Code
arr = [40, 100, 20, 30]
n = len(arr)
print(MaximumHeight(arr, n))
 
# This code is contributed by
# Sanjit_Prasad


C#
// C# program to find
// the maximum height of
// Pyramidal Arrangement
// of array values
using System;
 
class GFG
{
static int MaximumHeight(int []a,
                         int n)
{
    int result = 1;
    for (int i = 1; i <= n; ++i)
    {
 
        // Just checking whether
        // ith level is possible
        // or not if possible then
        // we must have atleast
        // (i*(i+1))/2 elements
        // in the array
        int y = (i * (i + 1)) / 2;
 
        // updating the result
        // value each time
        if (y < n)
            result = i;
         
        // otherwise we have
        // exceeded n value
        else
            break;
    }
    return result;
}
 
// Driver Code
static public void Main ()
{
    int []arr = {40, 100, 20, 30};
    int n = arr.Length;
    Console.WriteLine(MaximumHeight(arr, n));
}
}
 
// This code is contributed
// by m_kit


PHP


Javascript


C++
// CPP program to find the maximum height
// of Pyramidal Arrangement of array values
#include 
using namespace std;
 
int MaximumHeight(int a[], int n)
{
    return floor((-1+sqrt(1+(8*n)))/2);
}
 
int main()
{
    int arr[] = { 40, 100, 20, 30 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << MaximumHeight(arr, n);
    return 0;
}


Java
// Java program to find the maximum height
// of Pyramidal Arrangement of array values
import java.lang.*;
 
class GFG {
     
    static int MaximumHeight(int a[], int n)
    {
        return (int)Math.floor((-1 +
                Math.sqrt(1 + (8 * n))) / 2);
    }
     
    public static void main(String[] args)
    {
        int arr[] = new int[]{ 40, 100, 20, 30 };
        int n = arr.length;
         
        System.out.println(MaximumHeight(arr, n));
    }
}
 
// This code is contributed by Smitha


Python3
# Python program to find the
# maximum height of Pyramidal
# Arrangement of array values
import math
 
def MaximumHeight(a, n):
    return (-1 + int(math.sqrt(1 +
                    (8 * n)))) // 2
 
# Driver Code
arr = [40, 100, 20, 30]
n = len(arr)
print(MaximumHeight(arr, n))
 
# This code is contributed by
# Sanjit_Prasad


C#
// C# program to find the maximum height
// of Pyramidal Arrangement of array values
using System;
 
class GFG {
     
    static int MaximumHeight(int[]a, int n)
    {
        return (int)Math.Floor((-1 +
               Math.Sqrt(1 + (8 * n))) / 2);
    }
     
    public static void Main()
    {
        int []arr = new int[]{ 40, 100, 20, 30 };
        int n = 4;
         
        Console.Write(MaximumHeight(arr, n));
    }
}
 
// This code is contributed by Smitha


PHP


Javascript


输出:
2

时间复杂度: O(n)
空间复杂度: O(1)
我们可以在O(1)时间内解决此问题。我们简单地需要找到使得i *(i + 1)/ 2 <= n的最大值i。如果我们求解方程,我们得到floor((-1 + sqrt(1+(8 * n)))/ 2)

C++

// CPP program to find the maximum height
// of Pyramidal Arrangement of array values
#include 
using namespace std;
 
int MaximumHeight(int a[], int n)
{
    return floor((-1+sqrt(1+(8*n)))/2);
}
 
int main()
{
    int arr[] = { 40, 100, 20, 30 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << MaximumHeight(arr, n);
    return 0;
}

Java

// Java program to find the maximum height
// of Pyramidal Arrangement of array values
import java.lang.*;
 
class GFG {
     
    static int MaximumHeight(int a[], int n)
    {
        return (int)Math.floor((-1 +
                Math.sqrt(1 + (8 * n))) / 2);
    }
     
    public static void main(String[] args)
    {
        int arr[] = new int[]{ 40, 100, 20, 30 };
        int n = arr.length;
         
        System.out.println(MaximumHeight(arr, n));
    }
}
 
// This code is contributed by Smitha

Python3

# Python program to find the
# maximum height of Pyramidal
# Arrangement of array values
import math
 
def MaximumHeight(a, n):
    return (-1 + int(math.sqrt(1 +
                    (8 * n)))) // 2
 
# Driver Code
arr = [40, 100, 20, 30]
n = len(arr)
print(MaximumHeight(arr, n))
 
# This code is contributed by
# Sanjit_Prasad

C#

// C# program to find the maximum height
// of Pyramidal Arrangement of array values
using System;
 
class GFG {
     
    static int MaximumHeight(int[]a, int n)
    {
        return (int)Math.Floor((-1 +
               Math.Sqrt(1 + (8 * n))) / 2);
    }
     
    public static void Main()
    {
        int []arr = new int[]{ 40, 100, 20, 30 };
        int n = 4;
         
        Console.Write(MaximumHeight(arr, n));
    }
}
 
// This code is contributed by Smitha

的PHP


Java脚本


输出:
2

时间复杂度: O(1)
空间复杂度: O(1)