Maximize protected population with one-step guard shifts
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Online Assessment
Overview: This question evaluates the ability to reason about constrained local moves and optimize a numerical objective over an array, testing skills in array manipulation, combinatorial optimization, and interpretation of binary state representations.
Constraints
- 0 <= n <= 2 * 10^5
- len(population) == len(unit)
- 0 <= population[i] <= 10^9
- unit contains only '0' and '1'
Examples
Input: ([10, 5, 8, 9, 6], "01101")
Expected Output: 27
Input: ([10, 1, 2, 3], "0111")
Expected Output: 15
Hints
- Look at maximal consecutive blocks of '1's. Blocks separated by zeros can be optimized independently.
- If a block of guards is preceded by an empty city, only a prefix of that block can shift left. Think about which single city in that extended segment could be left unguarded.