#437 Prefix Intersection Size of Two Lists
Implement a function that calculates the sequence of intersection sizes for the prefixes of two identifier lists.
Algorithm description:
-
Two lists
top1andtop2of lengthNare given; -
At step
i, take prefixes of lengthifrom both lists, treat them as sets, and find the cardinality (size) of their intersection; - Numbers in the lists may repeat, but only unique elements are considered when calculating the intersection;
-
The result is a list of
Nnumbers, where each number is the size of the intersection of the corresponding prefixes.
The input lists are guaranteed to have equal lengths, so this does not need to be validated.
Examples:
Input: [1, 2, 3, 4], [3, 4, 5, 6]
Output: [0, 0, 1, 2]
Input: [1, 1, 1], [1, 1, 1]
Output: [1, 1, 1]
Input: [10, 7, 9], [1, 7, 3]
Output: [0, 1, 1]
Input: [3, 4, 5, 6], [1, 2, 3, 4]
Output: [0, 0, 1, 2]
Input: [1], [2]
Output: [0]
Question review
Sign in to start the dialogue
The mentor keeps the context of the current question and your previous messages.
Sign in
Available with Premium
This is just one of the solutions available with Premium. And more:
- 250+ advanced lectures
- 1300+ practice exercises
- 500+ solutions to problems from real interviews
- Personalized Java Hub certificates
- Expanded access to the AI mentor