Cell Absorption Simulation: Dominant Cell After Feeding Rounds
Arrays & HashingAuto-graded · 28 test casesLast updated
Simulate a row of cells that absorb weaker neighbors across repeated left-to-right rounds and identify the largest survivor at equilibrium.
Cell Absorption Simulation
A row of cells is placed side by side. Each cell belongs to a species (a single uppercase letter) and has a strength (a positive integer). Cells feed on their immediate neighbors according to the rules below. The feeding process continues in rounds until no cell can absorb another. At that point, return the species and strength of the strongest remaining cell.
Rules
Valid target. A neighbor qualifies as a valid target if all three conditions hold:
- It belongs to a different species than the attacker.
- Its current strength is strictly less than the attacker's current strength.
- It has not absorbed another cell during the current round.
Turn order within a round. Cells take turns from left to right, using the row as it stands at that exact moment. A cell that is absorbed before its own turn never takes one.
What happens on a turn. The active cell first checks its left neighbor. If that neighbor is a valid target, the cell absorbs it: the attacker's strength increases by the target's strength, and the target is removed from the row (closing the gap instantly). If the left neighbor does not qualify, the cell checks its right neighbor under the same conditions. If neither neighbor qualifies, the cell does nothing.
Immediate effect. Every absorption takes effect at once. Cells that act later in the same round see the updated strengths and updated adjacency.
Per-round limits. Each cell may absorb at most once per round. A cell that has absorbed during the current round cannot itself be absorbed for the remainder of that round.
Stopping condition. The simulation ends after the first round in which no absorption occurs.
Result. Return a string of the form "<species> <strength>", for example "B 15". The input guarantees that exactly one cell has the maximum strength when the simulation ends.
Constraints
1 <= len(cells) <= 20- Each species is a single uppercase English letter (
"A"to"Z"). 1 <= strength <= 10^6for every cell.- When the simulation ends, exactly one cell has the maximum strength.
Examples
Example 1
Input: dominant_cell_after_feeding([['A', 2], ['B', 5], ['C', 3], ['C', 4], ['A', 1]])
Output: 'B 15'
Example 2
Input: dominant_cell_after_feeding([['A', 1], ['B', 2], ['C', 10]])
Output: 'C 13'
Example 3
Input: dominant_cell_after_feeding([['A', 5], ['A', 2], ['B', 2], ['B', 6]])
Output: 'B 6'
Hints
Hint 1
Store each cell as a mutable object (e.g. a three-element list) so that strength updates and removals during a round are immediately visible to all later turns in the same round.
Hint 2
Walk the row with an explicit integer index rather than a for-each iterator. When a cell absorbs its left neighbor, that neighbor's slot is deleted and the absorber shifts one index to the left — do not increment the index in that case, just continue to the next iteration so the pointer lands on the absorber's right neighbor.
Hint 3
Track a boolean flag per cell indicating whether it has already absorbed something this round. Reset all flags at the start of each new round, and check the flag both to enforce the one-absorption limit and to grant immunity to cells that already acted.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Core Idea
With at most 20 cells, a faithful simulation is both correct and fast enough. There is no need for clever data structures — the entire challenge is translating the rules precisely into code. Three details trip up most implementations:
- The "absorbed this round" immunity flag.
- Iterating over a list that shrinks mid-loop.
- The index arithmetic when the left neighbor is absorbed.
Step-by-Step Plan
Mutable row. Convert each input tuple into a three-element list [species, strength, absorbed_flag]. Lists allow in-place strength updates and deletions that all later turns in the same round will see immediately.
Outer loop. Repeat rounds until a full round passes with no absorption. Use a changed flag reset to False at the start of each round, and reset every cell's absorbed_flag to False as well.
Inner traversal with an explicit index. A for loop over a shrinking list skips or revisits elements. An integer index i gives full control:
- Left check first. If
row[i-1]is a valid target, absorb it: increaseme.strength, setme.absorbed_flag = True, thendel row[i-1]. After the deletion the absorber slides from indexito indexi-1, and the value ofi(unchanged) now points at the absorber's right neighbor — exactly the cell that should take the next turn. Usecontinueto skip the right-check and avoid incrementingi. - Right check otherwise. If
row[i+1]is a valid target, absorb it: increase strength, set flag,del row[i+1]. The gap closes;i += 1at the bottom then moves to the new right neighbor. - No valid target. Just advance
i.
Result. Take the cell with the maximum strength and format it as "<species> <strength>".
Why the Rules Are Respected
| Rule | How the code handles it |
|---|---|
| Left neighbor checked before right | The left block runs first; the right block is only reached if the left check fails |
| Absorbed cell cannot be eaten this round | not left[2] / not right[2] in the validity check |
| Absorber can eat at most once per round | Each cell gets exactly one turn; once i passes it the cell is behind the pointer |
| Eaten cell gets no turn | It is physically deleted before i could ever reach its former position |
| Changes visible immediately | Strengths updated in place, deletions close the gap at once |
Termination: each productive round removes at least one cell, so at most n − 1 productive rounds occur before the final quiet round.
Complexity
- Time: O(n³) — at most n rounds, each with O(n) turns, each deletion O(n). Negligible for n ≤ 20.
- Space: O(n) for the working row.
Edge Cases
- Single cell: the first round changes nothing and the cell is returned immediately.
- All same species or all equal strengths: no valid target ever exists; the answer is the largest original cell.
- Equal strength across different species: strictly-less-than means
A 3cannot absorbB 3. - Immunity blocking a chain: a cell that grew this round is shielded until the next round (sample 2 —
C 10must wait for round 2 to absorbB 3). - Left-eat at position 1: after absorbing
row[0], the absorber moves to index 0.iremains 1, so the next turn correctly starts at index 1 (the absorber's right neighbor).
Reference Solution
from typing import List, Tuple
def dominant_cell_after_feeding(cells: List[Tuple[str, int]]) -> str:
# Convert input tuples into mutable lists: [species, strength, absorbed_this_round]
row = [[species, strength, False] for species, strength in cells]
while True:
# Reset the absorbed flag for every cell at the start of each round.
for cell in row:
cell[2] = False
changed = False
i = 0
while i < len(row):
me = row[i]
# 1) Try the left neighbor first.
if i > 0:
left = row[i - 1]
if left[0] != me[0] and left[1] < me[1] and not left[2]:
me[1] += left[1] # absorber grows
me[2] = True # mark as having absorbed this round
del row[i - 1] # target removed; absorber shifts to index i-1
changed = True
# After the deletion the absorber sits at i-1 and i still
# equals the old i, which is now the absorber's right neighbor.
# So do NOT increment i — just continue to start the next turn.
continue
# 2) Otherwise try the right neighbor.
if i + 1 < len(row):
right = row[i + 1]
if right[0] != me[0] and right[1] < me[1] and not right[2]:
me[1] += right[1] # absorber grows
me[2] = True # mark as having absorbed this round
del row[i + 1] # gap closes; new right neighbor slides into i+1
changed = True
# i += 1 below moves to the new right neighbor — correct.
i += 1
if not changed:
break
best = max(row, key=lambda cell: cell[1])
return f"{best[0]} {best[1]}"
Tracing Sample 1
Input: [A2, B5, C3, C4, A1]
Round 1
i=0(A2): no left neighbor; rightB5is larger. Nothing.i=1(B5): leftA2is a valid target. Absorbs it →B7. Row:[B7, C3, C4, A1].istays 1 (now points atC3).i=1(C3): leftB7is larger; rightC4is same species. Nothing.i→ 2.i=2(C4): leftC3is same species; rightA1is valid. Absorbs it →C5. Row:[B7, C3, C5].i→ 3, loop ends.
Round 2
B7absorbsC3→B10. Row:[B10, C5].C5cannot absorbB10.
Round 3
B10absorbsC5→B15. Row:[B15].
Round 4
- Nothing changes. Result:
"B 15".