You are given n cities arranged in a line. City i has population population[i] and may contain a security unit described by unit[i], where unit[i] = '1' means a unit is initially stationed in city i.
Each security unit may stay where it is, or if it is not in the first city, it may move exactly one city to the left. Every unit can move at most once.
After all moves are chosen, a city is protected if at least one security unit is stationed there. Return the maximum total population of all protected cities.
Complete the function maximizeProtectedPopulation in the editor below.
maximizeProtectedPopulation has the following parameters:
int[] population: the city populationsString unit: the initial security-unit layout
Returns
long: the maximum total population of protected cities.
population = [10, 5, 8, 9, 6] unit = "01101" return = 27
population = [7, 4] unit = "01" return = 7
Move the only unit left from city 2 to city 1. Protecting city 1 yields the larger total population.
population.length = unit.length()unitcontains only'0'and'1'- Each security unit may move left by at most one city, and may move at most once.
- Closest Version DateONSITE INTERVIEW · Seen Jul 2026
- Maximum Concurrent Processes (Bar Raiser Round)ONSITE INTERVIEW · Seen Jul 2026
- Maximum Product New RatingOA · Seen Jul 2026
- Permutation SorterOA · Seen Jul 2026
- Get Distinct Pairs (Also apply to AS intern)Seen Jul 2026
- Maximum Final ValueSeen Jul 2026
- Minimum Delivery Center InconvenienceOA · Seen Jun 2026
- Unfulfilled Customers by Inventory PriorityOA · Seen Jun 2026
public long maximizeProtectedPopulation(int[] population, String unit) {
// write your code here
}