Round 1
Questions: A biological researcher is examining bacteria interactions, where some bacteria are poisonous to others. The samples are arranged consecutively in a row from 1 to n. Given a list detailing which bacteria are poisonous to others, determine the number of intervals within the row that contain only samples capable of coexisting.
Example:
n = 3 allergic = [2, 1, 3] poisonous = [3, 3, 1]
To determine relationships, the elements of:
- poisonous[0] -> allergic[0] so 3 is poisonous to 2
- poisonous[1] -> allergic[1] so 3 is poisonous to 1
- poisonous[2] -> allergic[2] so 1 is poisonous to 3
The bacteria are arranged as 123. An interval must be contiguous and the samples may not be reordered, like a subarray. All of the possible intervals are (1), (2), (3), (1, 2), (2, 3), (1, 2, 3). Since the poisonous bacteria need to be kept apart from those that are allergic to them, the only valid intervals are (1), (2), (3), (1, 2). No intervals can contain both bacteria 1 and 3 or bacteria 2 and 3. There are 4 valid intervals.
Function Description: Complete the function bioHazard in the editor below. bioHazard has the following parameters:
- int n: the number of unique bacteria types
- int allergic[m]: allergic[i] is allergic to poisonous[i]
- int poisonous[m]: the toxic bacteria
Returns:
- int: the number of intervals made up only of bacteria that can coexist
Constraints:
- 1 ≤ n ≤ 10^5
- 1 ≤ m ≤ 10^6
- 1 ≤ allergic[i], poisonous[i] ≤ n
Sample Case 0:
n = 4 allergic = [1, 2] poisonous = [3, 4]
Sample Output:
7
I was not able to solve this problem, leetcode community kindly help me solve this problem.
Candidate's Approach
No approach provided.
Interviewer's Feedback
No feedback provided.