Spent the first few minutes just re-reading the problem because I wasn't sure if units could stack or if each city just needed one.
Model the problem as a greedy assignment where each security unit can protect its own city or the city to its left. Process cities from left to right, prioritizing protection for the city with the larger population when a unit can cover both. Use a simple rule: if a unit is present at city i, compare populations of city i and i-1, and assign the unit to the city with higher population, ensuring no city gets more than one unit.
Pro tip: Clarify that the greedy choice is optimal because each unit's decision only affects two adjacent cities and choosing the higher population never reduces future options. Also, mention that the solution runs in O(n) time and O(1) space, which is optimal for this problem.
Restate the problem: n cities in a row, each with a population and a binary string indicating security units. Each unit can move left by one or stay. Maximize total population of cities with at least one unit. Note that units are indistinguishable and each city can have at most one unit effectively.
For each unit at city i, it can protect city i or city i-1. If both are unprotected, assign the unit to the one with larger population. If one is already protected, assign to the other if possible. This local decision is optimal because it maximizes immediate gain without affecting other units.
Iterate through cities from left to right. Keep track of whether the previous city is protected. For each city i with a unit, if city i-1 is unprotected and its population is greater than city i's population, assign the unit to i-1; otherwise assign to i. Mark the chosen city as protected. Sum populations of protected cities.
Consider cases with no units, units at the first city (cannot move left), multiple units in a row, and cities with equal populations. Walk through a small example to verify the greedy choice yields the maximum sum.
State that the algorithm runs in O(n) time and O(1) extra space (besides input). Explain why greedy is optimal: each unit's decision is independent and choosing the higher population never prevents a better assignment later.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.