Count House Segments After Destruction
You are monitoring building density in a district of houses. The district is represented as a number line, and each house is located at an integer position.
You are given houses, an array containing the initial locations of all houses, and queries, an array containing the locations of houses destroyed in order. After each destroyed house, return the number of house segments remaining.
A house segment is one or more adjacent houses whose positions are consecutive integers and which do not have neighboring houses immediately outside the segment.
houses = [1, 2, 3, 6, 7, 9] queries = [6, 3, 7, 2, 9, 1] return = [3, 3, 2, 2, 1, 0]
Initially the house segments are [1, 2, 3], [6, 7], and [9]. Removing the houses in query order leaves 3, 3, 2, 2, 1, and then 0 segments.
houses = [2, 4, 5, 6, 7] queries = [5, 6, 2] return = [3, 3, 2]
After removing 5, the segments are [2], [4], and [6, 7]. After removing 6, the segments are [2], [4], and [7]. After removing 2, two segments remain.
- All values in
housesare distinct. - Every value in
queriesappears inhouses, and all values inqueriesare distinct.
- Count Numbers with Even Number of DigitsOA · Seen May 2026
- Laser Robot Safe PathOA · Seen May 2026
- Longest Same-Character SubstringOA · Seen May 2026
- Cyclic Shift to Strictly Descending ArrayOA · Seen May 2026
- Dynamic Wall Building and Range QueryOA · Seen May 2026
- Queue Check-in Simulation with Capacity LimitOA · Seen May 2026
- Get Function Execution TimeSeen Jan 2025
- Sum Digits Until OneSeen Dec 2024
public int[] countHouseSegmentsAfterDestruction(int[] houses, int[] queries) {
// write your code here
}