Largest Microorganism After Consumption
Problem statement
Microorganisms are arranged from left to right. Microorganism i has family families[i] and positive size sizes[i].
The simulation proceeds in rounds. At the start of each round, every surviving microorganism is eligible to participate once. Scan the current line from left to right:
- If the current microorganism and its immediate left neighbor have not participated this round, and the left neighbor is strictly smaller, the current microorganism eats the left neighbor.
- Otherwise, if the current microorganism and its immediate right neighbor have not participated this round, and the right neighbor is strictly smaller, the current microorganism eats the right neighbor.
- Otherwise, it does nothing.
When one microorganism eats another, the eater gains the target's size, the target disappears, and the eater cannot participate again in that round. Earlier removals immediately change which microorganisms are adjacent.
Examples
Example 1
families = ["A","B","C"]sizes = [3,1,2]return = "A 6"In the first round, A eats B and grows to 4. In the next round, A eats C and grows to 6.
Unlock this recently reported problem
FastPrep Pro gives you full access to interview problems reported within the last week.
- Full problem statement and constraints
- 2 more worked examples, explained
- Guided hints and editorial
- Run your code on real test cases
$99 billed yearly — or $19 month-to-month. Cancel anytime.