给定一个正整数N ,任务是从每个可能的二进制最大堆的最大叶节点中找到最大和最小元素,该最大叶节点取前N 个自然数作为二进制最大堆的节点值。
例子:
Input: N = 2
Output: 1 1
Explanation:
There is only one maximum binary heap with the nodes {1, 2}:
In the above tree, maximum leaf node value = 1.
Therefore, the largest element is 1 and the smallest element is also 1.
Input: N = 3
Output: 2 2
Explanation:
There are two possible maximum binary heaps with the nodes {1, 2, 3}:
The maximum leaf nodes of first and second heaps are 2 and 2 respectively.
Therefore, the largest element is 2 and the smallest element is also 2.
朴素的方法:最简单的方法是生成可以由前N 个自然数形成的所有可能的最大二元堆,并跟踪所有堆中所有最大叶节点中的最小和最大叶节点值。
时间复杂度: O(N*N!)
辅助空间: O(1)
高效的方法:上述方法可以基于以下观察进行优化:
- 最大堆是一棵完全二叉树,因此叶节点的高度和数量是固定的。
- 在最大堆中,节点的值总是大于或等于该节点的子节点。
- 最大叶节点值始终大于或等于树中的叶数。因此,如果将最小的数字放在叶节点上,则叶节点的最大值将被最小化。并且将等于叶子节点的数量。
- 最大堆的另一个属性是,当沿着树向下时,节点的值会减少。因此,如果将数字放置在具有最小可能深度的叶节点处,则节点的最大值将最大化。因此,如果D是该节点的深度,则该节点的最大可能值将等于ND。
- 如果D是最大堆的深度,则叶节点的可能深度仅为D和D-1 ,因为堆是完整的二叉树。
- 如果V是叶节点,则(2*V)必须大于N 。因此,叶节点的数量等于(N – N/2)。
请按照以下步骤解决问题:
- 如上所述,计算 N 个节点的最大堆中的叶节点数,并将其存储在变量中,例如numberOfleaves 。
numberOfleaves = (N- N/2).
- 找到最大堆的深度并将其存储在变量D 中。
D = ceil(log2(N+1))-1
- 如果 N+1 不是 2 的幂并且D大于1 ,则必须在深度D-1处退出叶节点。因此,然后将D减1。
- 最后,完成上述步骤后,将最大值打印为(ND) ,将最小值打印为numberOfleaves 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find largest and smallest
// elements from the maximum leaf nodes
// values from all possible binary max heap
void leafNodeValues(int N)
{
// Stores count of leaf nodes
int numberOfLeaves = (N - N / 2);
// Stores minDepth with N
int minDepth = ceil(log2(N + 1)) - 1;
// Increment N by 1
N++;
// If N is not power of 2 and minDepth
// is greater than 1
if (minDepth > 1 && (N & (N - 1)))
minDepth--;
// Print the smallest and largest possible
// value of a leaf node
cout << numberOfLeaves << ' '
<< N - minDepth - 1;
}
// Driver Code
int main()
{
// Given Input
int N = 2;
// Function Call
leafNodeValues(N);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to find largest and smallest
// elements from the maximum leaf nodes
// values from all possible binary max heap
static void leafNodeValues(int N)
{
// Stores count of leaf nodes
int numberOfLeaves = (N - N / 2);
// Stores minDepth with N
int minDepth = (int)Math.ceil(Math.log(N + 1) /
Math.log(2)) - 1;
// Increment N by 1
N++;
// If N is not power of 2 and minDepth
// is greater than 1
if (minDepth > 1 && (N & (N - 1)) != 0)
minDepth--;
// Print the smallest and largest possible
// value of a leaf node
System.out.println(numberOfLeaves + " " +
(N - minDepth - 1));
}
// Driver code
public static void main(String[] args)
{
// Given Input
int N = 2;
// Function Call
leafNodeValues(N);
}
}
// This code is contributed by divyesh072019
Python3
# Python 3 program for the above approach
from math import ceil,log2
# Function to find largest and smallest
# elements from the maximum leaf nodes
# values from all possible binary max heap
def leafNodeValues(N):
# Stores count of leaf nodes
numberOfLeaves = (N - N // 2)
# Stores minDepth with N
minDepth = ceil(log2(N + 1)) - 1;
# Increment N by 1
N += 1
# If N is not power of 2 and minDepth
# is greater than 1
if (minDepth > 1 and (N & (N - 1))):
minDepth -= 1
# Print the smallest and largest possible
# value of a leaf node
print(numberOfLeaves, N - minDepth - 1)
# Driver Code
if __name__ == '__main__':
# Given Input
N = 2
# Function Call
leafNodeValues(N)
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program for the above approach
using System;
class GFG {
// Function to find largest and smallest
// elements from the maximum leaf nodes
// values from all possible binary max heap
static void leafNodeValues(int N)
{
// Stores count of leaf nodes
int numberOfLeaves = (N - N / 2);
// Stores minDepth with N
int minDepth = (int)Math.Ceiling(Math.Log(N + 1) /
Math.Log(2)) - 1;
// Increment N by 1
N++;
// If N is not power of 2 and minDepth
// is greater than 1
if (minDepth > 1 && (N & (N - 1)) != 0)
minDepth--;
// Print the smallest and largest possible
// value of a leaf node
Console.WriteLine(numberOfLeaves + " " +
(N - minDepth - 1));
}
static void Main()
{
// Given Input
int N = 2;
// Function Call
leafNodeValues(N);
}
}
// This code is contributed by decode2207.
Javascript
1 1
时间复杂度: O(log(N))
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。