Find the shortest length of the sequence from the list of arrays that should contain an element from each array

Here is the problem:

Suppose we have the following arrays:

[1,2,9]
[3,6,7]
[4,11]
[8,10,12]

The elements of the array are unique and ordered. The task is to find the shortest length of the sequence, which will contain at least one element of each array, but these elements must go one after another (without spaces, cannot be [1,3]), and also ordered. Therefore, in this case, the answer is:

5 => [7,8,9,10,11]

Is there an effective way to do this?

+4
source share
2 answers

This can be easily resolved as shown below.

  • List item
  • initialize smallest_range as MAX_INT
  • 3 / p1, p2 p3, L1, L2 L3 .
  • , / p1, p2 p3
  • , 3, . smallest_range , .
  • / , 3.
  • 3 5, / .

Alse . . ++ java.

+2

(, ). O(nlogn), n - .

1d-, . ,

struct A{
    int value;
    int array_id;
};

(O(nlogn)). , , . . O(n).

, O(nlogn).

+2

Source: https://habr.com/ru/post/1612910/


All Articles