Problem · Array

Neighboring House Groups

Learn this problem
MediumAtlassian logoAtlassianFULLTIMEOA

Problem statement

Houses occupy distinct integer coordinates on a straight line. Two remaining houses are neighbors when their coordinates differ by exactly 1. A neighboring group is a maximal set of remaining houses connected by this relation.

For each value in instructions, delete that house and append the current number of neighboring groups to the answer. Every instruction names a house that is still present.

Return the group counts in instruction order.

Function

neighboringHouseGroups(houses: int[], instructions: int[]) → int[]

Examples

Example 1

houses = [2,1,3,4,6,7,9]instructions = [6,3,9]return = [3,4,3]

After deleting 6, the groups are [1,2,3,4], [7], and [9]. Deleting 3 splits the first group, and deleting 9 removes one singleton group.

Constraints

  • 1 <= houses.length <= 200000
  • 1 <= instructions.length <= houses.length
  • -10^9 <= houses[i] <= 10^9
  • House coordinates are distinct.
  • Every instruction names a currently present house.

More Atlassian problems

drafts saved locally
public int[] neighboringHouseGroups(int[] houses, int[] instructions) {
  // Write your code here.
}
houses[2,1,3,4,6,7,9]
instructions[6,3,9]
expected[3,4,3]
checking account