Problem Β· Intervals

Combine Two Vectors of Intervals (MLE) πŸ™Œ

Learn this problem
● MediumMetaOA
See Meta hiring insights

Problem statement

Imagine you have two vectors of intervals, labeled as X and Y, each representing a series of segments along a path. In vector X, there are no overlapping segments, and the same goes for vector Y. Your task is to merge these two vector into a single vector, ensuring that there are no overlaps in the resulting segments. However, you're asked to devise a highly efficient solution that outperforms naive methods.

Function

combineTwoVectorsOfIntervals(X: int[][], Y: int[][]) β†’ int[][]

Complete the function combineTwoVectorsOfIntervals in the editor πŸ‘‰.

Returns

int[][]: The merged vector of intervals without any overlaps.

P.S. For original prompt, pls refer to source image. πŸ’š

Examples

Example 1

X = [[1,5], [10,14], [16,18]]Y = [[2,6], [8,10], [11,20]]return = [[1,6], [8,20]]
The intervals from list X and list Y are merged and the overlapping intervals are combined to produce the following output:
  • The intervals [1,5] and [2,6] overlap and can be merged into [1,6].
  • The intervals [10,14] and [11,20] overlap with [8,10], and all can be merged into [8,20].
  • The interval [16,18] is within the bounds of [11,20] and is therefore already covered by it.
The resulting merged list of intervals with no overlaps is [[1,6], [8,20]]. (Provided by Groot with 🫢 from the west coast. PLS dont hesitate to correct me if you find anything wrong. I am always on the discord serva πŸ›΅)

Constraints

An unknown myth for now

More Meta problems

drafts saved locally
public int[][] combineTwoVectorsOfIntervals(int[][] X, int[][] Y) {
    // write your code here
}
X[[1,5], [10,14], [16,18]]
Y[[2,6], [8,10], [11,20]]
expected[[1,6], [8,20]]
checking account