📜  给定矩形框形成的金字塔的高度

📅  最后修改于: 2021-05-04 14:11:37             🧑  作者: Mango

给定具有长度m的尺寸{l,b}的矩形块的阵列。我们可以通过修剪一个或两个尺寸来将每个矩形块成形为单个正方形块。用这些正方形块形成金字塔。我们的任务是找到可以形成的金字塔的最大高度。
笔记:
通过在底部放置一个正方形块(例如N * N)来构建金字塔,在其顶部放置另一个正方形块N-1 * N-1,在其顶部放置一个N-2 * N-2正方形块依此类推,最后以1 * 1块结束。这样的金字塔的高度是N。

例子:

方法:

  • 我们必须将每个块的尺寸
    l * l or b * b
  • 由于我们只能修剪而不能合并,因此我们选择正方形块的尺寸为min(l,b)
  • 在每个矩形块中使用MIN(l,b)创建一个数组a
  • 对数组a排序,并将height初始化为0
  • 如果数组中的元素大于height + 1,则遍历数组a
    然后将height的值增加1
  • 返回高度
C++
#include 
#include 
  
using namespace std;
  
// Function to return
// The height of pyramid
int pyramid(int n, int arr[][2])
{
    vector a;
    int height = 0;
    for (int i = 0; i < n; i++) {
        // For every ith array
        // append the minimum value
        // between two elements
        a.push_back(min(arr[i][0], arr[i][1]));
    }
    sort(a.begin(), a.end());
    for (int i = 0; i < a.size(); i++) {
        // if the element in array is
        // greater than height then
        // increment the value the
        // value of height by 1
        if (a[i] > height)
            height++;
    }
    return height;
}
  
// Driver code
int main()
{
    int n = 4;
    int arr[][2] = { { 8, 8 },
                     { 8, 2 },
                     { 4, 2 },
                     { 2, 1 } };
    cout << "Height of pyramid is "
         << pyramid(n, arr);
}


Java
import java.util.*;
  
public class Gfg {
  
    static int pyramid(int n, int arr[][])
    {
        int[] a = new int[n];
        int height = 0;
  
        for (int i = 0; i < n; i++) {
  
            // For every ith array
            // append the minimum value
            // between two elements
            a[i] = arr[i][0] < arr[i][1]
                       ? arr[i][0]
                       : arr[i][1];
        }
  
        // sorting the array
        Arrays.sort(a);
        for (int i = 0; i < n; i++) {
  
            // if the element in array is
            // greater than height then
            // increment the value the
            // value of height by 1
            if (a[i] > height)
                height++;
        }
        return height;
    }
    public static void main(String[] args)
    {
        int n = 4;
        int arr[][] = { { 8, 8 },
                        { 8, 2 },
                        { 4, 2 },
                        { 2, 1 } };
        System.out.println("Height of pyramid is "
                           + pyramid(n, arr));
    }
}


Python
# Function to return
# The height of pyramid
def pyramid(n, arr):
    # Empty array
    a = []
    height = 0
    for i in arr:
        # For every ith array
        # append the minimum value
        # between two elements
        a.append(min(i))
    # Sort the array
    a.sort()
    # Traverse through the array a
    for i in range(len(a)):
        # if the element in array is
        # greater than height then
        # increment the value the
        # value of height by 1
        if a[i] > height:
            height = height + 1
  
    return height
# Driver code
if __name__ == '__main__':
    n = 4
    arr = [[8, 8], [2, 8], [4, 2], [2, 1]]
    print("Height of pyramid is", pyramid(n, arr))


C#
using System;
  
class Gfg 
{ 
  
    static int pyramid(int n, int [,]arr) 
    { 
        int[] a = new int[n]; 
        int height = 0; 
  
        for (int i = 0; i < n; i++)
        { 
  
            // For every ith array 
            // append the minimum value 
            // between two elements 
            a[i] = arr[i, 0] < arr[i, 1] 
                    ? arr[i, 0] 
                    : arr[i, 1]; 
        } 
  
        // sorting the array 
        Array.Sort(a); 
        for (int i = 0; i < n; i++) 
        { 
  
            // if the element in array is 
            // greater than height then 
            // increment the value the 
            // value of height by 1 
            if (a[i] > height) 
                height++; 
        } 
        return height; 
    } 
      
    // Driver code
    public static void Main() 
    { 
        int n = 4; 
        int [,]arr = { { 8, 8 }, 
                        { 8, 2 }, 
                        { 4, 2 }, 
                        { 2, 1 } }; 
          
        Console.WriteLine("Height of pyramid is "
                        + pyramid(n, arr)); 
    } 
} 
  
// This code is contributed by AnkitRai01


输出:
Height of pyramid is  3