Count House Segments After Destruction
Learn this problemProblem statement
You are monitoring the building density in a district of houses. The district is represented as a number line, where each house is located at an integer position. Some of the houses are gradually destroyed over time.
You are given houses, an array of integers representing the initial locations of all houses in the district. You are also given queries, an array of integers representing the locations of houses that will be destroyed, in the order in which they are destroyed.
After each house is destroyed, find the number of house segments remaining in the district. A house segment is a maximal group of one or more houses at consecutive integer positions.
Return an array of integers representing the number of house segments after each respective house from queries is destroyed.
Function
solution(houses: int[], queries: int[]) → int[]Examples
Example 1
houses = [1, 2, 3, 6, 7, 9]queries = [6, 3, 7, 2, 9, 1]return = [3, 3, 2, 2, 1, 0]The initial house segments are [1, 2, 3], [6, 7], and [9]. Destroying houses in the order [6, 3, 7, 2, 9, 1] leaves [3, 3, 2, 2, 1, 0] segments after the respective operations.
Constraints
- All house locations in
housesare distinct. - Every house location in
queriesis present inhouses. - All house locations in
queriesare distinct.