📜  从给定的对象数组中找到最大高度金字塔

📅  最后修改于: 2021-10-26 05:58:24             🧑  作者: Mango

给定n 个对象,每个对象的宽度为w i 。我们需要以金字塔的方式排列它们,以便:

  1. i th 的总宽度小于 (i + 1) th
  2. 第 i对象的总数小于 (i + 1) th 个

任务是找到从给定物体可以达到的最大高度。
例子 :

Input : arr[] = {40, 100, 20, 30}
Output : 2
Top level : 30.
Lower (or bottom) level : 20, 40 and 100
Other possibility can be placing
20 on the top, and at second level any
other 4 objects. Another possibility is
to place 40 at top and other three at the
bottom.

Input : arr[] = {10, 20, 30, 50, 60, 70}
Output : 3

这个想法是使用贪婪的方法,将宽度最小的对象放在顶部,下一个对象放在正下方的级别,依此类推。
要找到最大级别数,请对给定数组进行排序并尝试从上到下形成金字塔。排序后找到数组的最小元素即数组的第一个元素,放在最上面。然后尝试在其下方构建具有更多对象和更大宽度的关卡。
下面是这个方法的实现:

C++
// C++ program to find maximum height pyramid
// from the given object width.
#include
using namespace std;
 
// Returns maximum number of pyramidcal levels
// n boxes of given widths.
int maxLevel(int boxes[], int n)
{
    // Sort objects in increasing order of widths
    sort(boxes, boxes + n);
 
    int ans = 1;  // Initialize result
 
    // Total width of previous level and total
    // number of objects in previous level
    int prev_width = boxes[0];
    int prev_count = 1;
 
    // Number of object in current level.
    int curr_count = 0;
 
    // Width of current level.
    int curr_width = 0;
    for (int i=1; i prev_width &&
            curr_count > prev_count)
        {
            // Update previous width, number of
            // object on previous level.
            prev_width = curr_width;
            prev_count = curr_count;
 
            // Reset width of current level, number
            // of object on current level.
            curr_count = 0;
            curr_width = 0;
 
            // Increment number of level.
            ans++;
        }
    }
 
    return ans;
}
 
// Driver Program
int main()
{
    int boxes[] = {10, 20, 30, 50, 60, 70};
    int n = sizeof(boxes)/sizeof(boxes[0]);
    cout << maxLevel(boxes, n) << endl;
    return 0;
}


Java
// Java program to find maximum height pyramid
// from the given object width.
import java.io.*;
import java.util.Arrays;
 
class GFG {
     
    // Returns maximum number of pyramidcal
    // levels n boxes of given widths.
    static int maxLevel(int []boxes, int n)
    {
 
        // Sort objects in increasing order
        // of widths
        Arrays.sort(boxes);
     
        int ans = 1; // Initialize result
     
        // Total width of previous level
        // and total number of objects in
        // previous level
        int prev_width = boxes[0];
        int prev_count = 1;
     
        // Number of object in current
        // level.
        int curr_count = 0;
     
        // Width of current level.
        int curr_width = 0;
        for (int i = 1; i < n; i++)
        {
            // Picking the object. So
            // increase current width
            // and number of object.
            curr_width += boxes[i];
            curr_count += 1;
     
            // If current width and
            // number of object
            // are greater than previous.
            if (curr_width > prev_width &&
                curr_count > prev_count)
            {
                 
                // Update previous width,
                // number of object on
                // previous level.
                prev_width = curr_width;
                prev_count = curr_count;
     
                // Reset width of current
                // level, number of object
                // on current level.
                curr_count = 0;
                curr_width = 0;
     
                // Increment number of
                // level.
                ans++;
            }
        }
     
        return ans;
    }
     
    // Driver Program
    static public void main (String[] args)
    {
        int []boxes = {10, 20, 30, 50, 60, 70};
        int n = boxes.length;
        System.out.println(maxLevel(boxes, n));
    }
}
 
// This code is contributed by anuj_67.


Python 3
# Python 3 program to find
# maximum height pyramid from
# the given object width.
 
# Returns maximum number
# of pyramidcal levels n
# boxes of given widths.
def maxLevel(boxes, n):
     
    # Sort objects in increasing
    # order of widths
    boxes.sort()
 
    ans = 1 # Initialize result
 
    # Total width of previous
    # level and total number of
    # objects in previous level
    prev_width = boxes[0]
    prev_count = 1
 
    # Number of object in
    # current level.
    curr_count = 0
 
    # Width of current level.
    curr_width = 0
    for i in range(1, n):
 
        # Picking the object. So
        # increase current width
        # and number of object.
        curr_width += boxes[i]
        curr_count += 1
 
        # If current width and
        # number of object are
        # greater than previous.
        if (curr_width > prev_width and
            curr_count > prev_count):
 
            # Update previous width,
            # number of object on
            # previous level.
            prev_width = curr_width
            prev_count = curr_count
 
            # Reset width of current
            # level, number of object
            # on current level.
            curr_count = 0
            curr_width = 0
 
            # Increment number of level.
            ans += 1
    return ans
 
# Driver Code
if __name__ == "__main__":
    boxes= [10, 20, 30, 50, 60, 70]
    n = len(boxes)
    print(maxLevel(boxes, n))
 
# This code is contributed
# by ChitraNayal


C#
// C# program to find maximum height pyramid
// from the given object width.
using System;
 
public class GFG {
     
    // Returns maximum number of pyramidcal
    // levels n boxes of given widths.
    static int maxLevel(int []boxes, int n)
    {
        // Sort objects in increasing order
        // of widths
        Array.Sort(boxes);
     
        int ans = 1; // Initialize result
     
        // Total width of previous level
        // and total number of objects in
        // previous level
        int prev_width = boxes[0];
        int prev_count = 1;
     
        // Number of object in current
        // level.
        int curr_count = 0;
     
        // Width of current level.
        int curr_width = 0;
        for (int i=1; i prev_width &&
                   curr_count > prev_count)
            {
                 
                // Update previous width,
                // number of object on
                // previous level.
                prev_width = curr_width;
                prev_count = curr_count;
     
                // Reset width of current
                // level, number of object
                // on current level.
                curr_count = 0;
                curr_width = 0;
     
                // Increment number of
                // level.
                ans++;
            }
        }
     
        return ans;
    }
     
    // Driver Program
    static public void Main ()
    {
        int []boxes = {10, 20, 30, 50, 60, 70};
        int n = boxes.Length;
        Console.WriteLine(maxLevel(boxes, n));
    }
}
 
// This code is contributed by anuj_67.


PHP
 $prev_width and
            $curr_count > $prev_count)
        {
            // Update previous width, number
            // of object on previous level.
            $prev_width = $curr_width;
            $prev_count = $curr_count;
 
            // Reset width of current
            // level, number of object
            // on current level.
            $curr_count = 0;
            $curr_width = 0;
 
            // Increment number of level.
            $ans++;
        }
    }
    return $ans;
}
 
// Driver Code
$boxes = array(10, 20, 30, 50, 60, 70);
$n = count($boxes);
echo maxLevel($boxes, $n) ;
 
// This code is contributed by anuj_67.
?>


Javascript


输出 :

3

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