Problem · Binary Search
Build Blocks from a Starting Position
Learn this problemProblem statement
Given an infinite number line, you would like to build few blocks and obstacles on it. Specifically, you have to implement code which supports two types of operations:
[1, x]- builds an obstacle at coordinatexalong the number line. It is guaranteed that coordinatexdoes not contain any obstacles when the operation is performed.[2, x, size]- checks whether it's possible to build a block of sizesizebeginning at positionx. For example, forsize = 2andx = 0, it will check0and1on the number line for obstacles. Returns1if it is possible, i.e. there are no obstacles at the occupied coordinates, and return0otherwise. Please note that this operation does not actually build the block, it only checks whether a block can be built.
Given an array of operations containing both types of operations above, your task is to return a binary string representing the outputs for all [2, x, size] operations.
Function
buildBlocksFromPosition(operations: int[][]) → StringExamples
Example 1
operations = [[1, 2], [1, 5], [2, 3, 2], [2, 3, 3], [2, 1, 1], [2, 1, 2]]return = "1010"For
operations = [[1, 2],
[1, 5],
[2, 3, 2],
[2, 3, 3],
[2, 1, 1],
[2, 1, 2]]FastPrep-authored deterministic derivation: The source image is cropped before the example output. Applying the visible rules gives solution(operations) = "1010".
Constraints
- FastPrep execution-adapter constraints (not shown in the source image):
- Each operation is either
[1, x]or[2, x, size]. - Every
sizein a type-2 operation is positive. - Each type-1 operation targets a coordinate that does not currently contain an obstacle.