leetcode
leetcode 1751 ~ 1800
每段建筑物的平均高度

每段建筑物的平均高度

难度:

标签:

题目描述

代码结果

运行时间: 335 ms, 内存: 86.5 MB


/*
 * Problem: Given an array of building heights, find the average height of each building block.
 * Solution: Use Java Streams to calculate the sum and count, then find the average.
 */
import java.util.Arrays;
public class AverageBuildingHeightStream {
    public static double findAverageHeight(int[] heights) {
        return Arrays.stream(heights).average().orElse(0);
    }
}

解释

方法:

The solution uses a line sweep algorithm combined with differential counting. For each building, start and end positions are marked in a 'diff' dictionary where heights are added at the start and subtracted at the end. Another 'cnts' dictionary tracks how many buildings start or end at each point. By iterating over the sorted keys of these dictionaries, we compute the current total height and count of buildings at each position. This allows us to determine the average height for any segment where it remains constant. The result is appended to the list if the average height changes from the previous segment or a new segment starts.

时间复杂度:

O(k log k)

空间复杂度:

O(k)

代码细节讲解

🦆
如何保证在使用`diff`和`cnts`字典时,所有建筑的开始和结束位置都被正确处理,没有遗漏或重复?
通过为每个建筑的开始和结束位置在`diff`和`cnts`字典中分别增加和减少相应的高度和计数,可以确保没有遗漏或重复。每次循环添加建筑时,都会精确地修改这两个字典,因此只要输入的建筑数据是准确的,就能保证每个建筑的开始和结束都被正确地记录。此外,通过使用字典的键来唯一标识每个位置点,避免了重复处理同一位置的问题。
🦆
在计算平均高度时,如果`cnt`为零会怎样处理?即没有建筑物覆盖的区间应如何表示?
在实现中,如果`cnt`(即当前位置的建筑数量)为零,这意味着在该位置没有建筑物覆盖。在算法中,这种情况通常会跳过处理,因为没有建筑物覆盖的区间不需要计算平均高度。因此,这种区间不会在结果列表`ans`中表示。应当注意确保在两个有建筑物的有效区间之间的无覆盖区间不会被错误地计入结果。
🦆
当两个或多个建筑物在某些点重叠时,如何确保`diff`字典和`cnts`字典能正确反映所有建筑的总高度和数量?
当多个建筑物在同一位置重叠时,`diff`字典通过累加或累减每个建筑的高度变化来正确计算该点的总高度变化。同样,`cnts`字典通过对每个建筑在该点的开始和结束进行计数调整,确保正确地反映了重叠的建筑数量。因此,通过对每个建筑独立处理并更新这两个字典,算法可以准确地处理并记录重叠区域的总高度和建筑数量。
🦆
在最后一个位置处理结束后,如何确保最后一个区间被正确添加到结果列表`ans`中,尤其是当最后一个区间的平均高度与之前的不同时?
在算法的实现中,每次迭代结束时,都会检查是否需要将当前计算得到的区间添加到结果列表`ans`中。当迭代到最后一个位置时,如果存在未处理的有效区间(即`last`非空且`cnt`不为零),算法会进行一次最后的检查,确保这个区间被加入`ans`。这包括比较当前区间的平均高度是否与前一个区间不同,如果不同,则将其作为新区间添加到结果中。这样可以确保所有有效的区间都被正确记录,包括最后一个区间。

相关问题