Alternating Chain Assembly from Both Ends
Arrays & HashingAuto-graded · 22 test casesLast updated
Tests two-pointer simulation with alternating turns, conditional greedy selection rules, tie-breaking logic, and strict-increase constraints.
Problem Statement
You are given a row of planks, represented as an array of positive integers planks, where planks[i] is the length of the i-th plank. Two builders, Builder 1 and Builder 2, take turns pulling planks from this row to assemble a chain, with Builder 1 going first.
On every turn, the active builder may only take from one of the two ends of the remaining row — the leftmost or the rightmost plank.
The chain has one rule: each plank added must be strictly longer than the one placed just before it. The very first plank placed has no minimum requirement.
Selection Rules
Each builder follows a fixed decision procedure on their turn:
- Determine which ends are eligible: an end is eligible if its plank length is strictly greater than the length of the most recently placed plank.
- If both ends are eligible:
- Builder 1 takes the shorter of the two eligible ends. On a tie, Builder 1 takes the left end.
- Builder 2 takes the longer of the two eligible ends. On a tie, Builder 2 takes the right end.
- If exactly one end is eligible, the active builder must take that end regardless of identity.
- If neither end is eligible, or no planks remain, assembly halts immediately.
What to Return
Simulate the entire process and return a tuple (count_1, count_2, chain) where:
count_1— total planks collected by Builder 1count_2— total planks collected by Builder 2chain— the plank lengths in the order they were added to the chain
Constraints
1 <= len(planks) <= 2 * 10^51 <= planks[i] <= 10^9- Expected time complexity:
O(n) - Expected auxiliary space complexity:
O(n)for the output chain
Examples
Example 1
Input: assemble_plank_chain([3, 1, 5, 2, 4])
Output: [1, 1, [3, 4]]
Example 2
Input: assemble_plank_chain([2, 6, 3, 5, 4])
Output: [1, 1, [2, 6]]
Example 3
Input: assemble_plank_chain([5, 2, 5])
Output: [1, 0, [5]]
Hints
Hint 1
Track just four pieces of state as you iterate: a left pointer, a right pointer, the length of the last plank placed (start it at 0 so the first pick is always valid), and whose turn it is.
Hint 2
Separate the validity check from the preference logic. First figure out which ends are eligible, then — only if both are eligible — apply the builder's tie-breaking rule.
Hint 3
Builder 1 picks left when left_value <= right_value; Builder 2 picks left when left_value > right_value. When only one end is eligible, neither builder has a choice. The single-plank case (left == right) resolves correctly without special handling.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem is a faithful simulation — there is no shortcut or dynamic-programming insight hiding inside it. The rules fully determine every pick, so the answer is to execute those rules precisely in a single linear pass.
State
All the information needed at any point in the simulation fits in six variables:
left,right— indices of the two reachable ends of the remaining rowlast— length of the most recently placed plank (initialised to0; since every plank is at least1, the very first pick is always eligible)turn—0for Builder 1,1for Builder 2count_1,count_2,chain— the outputs being built
Per-turn logic
Each iteration of the main loop does exactly three things:
- Classify. Compute
left_valid = planks[left] > lastandright_valid = planks[right] > last. - Decide. If neither end is valid, break. If only one is valid, take it. If both are valid, apply the active builder's preference:
- Builder 1 takes left when
lv <= rv(shorter first; equal lengths fall left). - Builder 2 takes left when
lv > rv(longer first; equal lengths fall right).
- Builder 1 takes left when
- Commit. Advance the chosen pointer, append the plank to
chain, updatelast, credit the active builder, and flipturn.
Why this is correct
The process is fully deterministic: on every turn exactly one action is permitted by the rules, and the loop body computes that action.
- Separating the validity check from the preference check keeps the two concerns from tangling. Builder identity only matters inside the both-valid branch.
- The tie-break operators encode the rules directly:
<=gives Builder 1 the left plank on a tie; strict>gives Builder 2 the right plank on a tie. - The single-plank case (
left == right) needs no special handling. When one plank remains,lvandrvare the same value. If it is invalid both flags are false and the loop breaks. If it is valid both flags are true; Builder 1'slv <= rvis true (equal), Builder 2'slv > rvis false, so each correctly consumes that one plank and the pointers cross. - The strictly increasing property holds by construction: a plank is appended only when it exceeds
last, andlastis immediately updated to that plank.
Complexity
- Time: O(n). Each iteration either breaks immediately or consumes one plank (advancing one pointer). There are at most
niterations, each doing O(1) work. - Space: O(n) for the
chainoutput. All other variables are O(1) scalars.
Reference Solution
from typing import List, Tuple
def assemble_plank_chain(planks: List[int]) -> Tuple[int, int, List[int]]:
left = 0
right = len(planks) - 1
last = 0 # sentinel: every plank length (>= 1) exceeds it
turn = 0 # 0 = Builder 1, 1 = Builder 2
count_1 = 0
count_2 = 0
chain = []
while left <= right:
lv = planks[left]
rv = planks[right]
left_valid = lv > last
right_valid = rv > last
if not left_valid and not right_valid:
break
if left_valid and right_valid:
if turn == 0:
# Builder 1: prefer shorter; tie goes left (lv <= rv covers tie)
choose_left = lv <= rv
else:
# Builder 2: prefer longer; tie goes right (strict > needed)
choose_left = lv > rv
else:
# forced: take whichever end is eligible
choose_left = left_valid
if choose_left:
value = planks[left]
left += 1
else:
value = planks[right]
right -= 1
chain.append(value)
last = value
if turn == 0:
count_1 += 1
else:
count_2 += 1
turn ^= 1
return count_1, count_2, chain