My first instinct was greedy from left to right, which was wrong.
Model the problem as a greedy assignment where each city with a unit can either stay or move left to cover an adjacent city. Process cities from left to right, prioritizing covering the highest population city that can be protected without sacrificing future opportunities. Use a dynamic programming or greedy strategy to maximize the sum of populations of covered cities.
Pro tip: Clarify that each unit can move at most once and only left, so a unit at index i can cover i or i-1. Emphasize that you must avoid double-counting and ensure each unit is used at most once, which is a common pitfall.
Each unit can either stay in its original city or move one step left, covering at most two possible cities. No unit can move right or more than one step.
For each unit at index i, it can protect city i (if it stays) or city i-1 (if it moves left). If i=0, it can only stay.
A greedy approach from left to right works: at each city, decide whether to use an available unit to cover it, preferring to cover higher populations when possible. Alternatively, use DP with states tracking whether the current city is covered by a unit from the right.
Iterate through cities, maintaining a flag for whether a unit is available to move left. Handle cases where multiple units compete for the same city, and ensure each unit is used only once.
Test with small cases like populations [10,20,30] and units '101' to ensure the algorithm picks the optimal set of protected cities.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.