Leetcode Problem 2015. Average Height of Buildings in Each Segment

2015. Average Height of Buildings in Each Segment

Leetcode Solutions

Sweep Line Algorithm

  1. Create a list of events for each start and end of the buildings, marking the start with a positive height and the end with a negative height.
  2. Sort the events based on their position on the street.
  3. Initialize variables to keep track of the current sum of heights, the count of buildings, and the start of the current segment.
  4. Iterate through the sorted events, updating the sum and count accordingly.
  5. Calculate the average height at each point and determine if a new segment should be started or if the current segment should be extended.
  6. Handle the case where the count of buildings drops to zero, indicating a gap with no buildings.
  7. Continue processing until all events have been handled.
  8. Return the list of segments representing the street.
UML Thumbnail

Map-based Segment Tracking

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...