Problem · Array

Increase Solar-Powered Bulbs to the Maximum

Learn this problem
EasyAmazonNEW GRADOA
See Amazon hiring insights

Problem statement

Solar power consumption has increased due to growing concern over global warming. Some offices have decided to replace some of their electricity-powered bulbs with solar-powered bulbs. Here, an electricity-powered bulb uses a wired supply of electricity, while a solar-powered bulb is powered by an independent solar panel.

In one such office, the lights are arranged sequentially and represented by a binary string lights. Electricity-powered bulbs are represented by '0', and solar-powered bulbs are represented by '1'.

It is desired to have as many solar-powered bulbs as possible, but the electricity-powered ones are brighter than the solar-powered bulbs. An electricity-powered bulb can be replaced by a solar-powered bulb only if the new solar-powered bulb does not become adjacent to another solar-powered bulb. More formally, a '0' can be replaced by a '1' if and only if it does not become directly adjacent to another '1' after the replacement.

Note that the initial arrangement may already contain adjacent solar-powered bulbs; this constraint applies only to newly added bulbs.

Find the maximum number of solar-powered bulbs that can be present in the office after making replacements according to these conditions.

  • Report the total number of solar-powered bulbs in the final arrangement, not just the number of bulbs that were replaced.

Parameter:

  • String lights: a string representing the current arrangement of bulbs.

Returns: an int equal to the maximum total number of solar-powered bulbs that can be present after replacements.

Function

IncreaseSolarPoweredBulbs(lights: String) → int

Examples

Example 1

lights = "1111110001"return = 8

Initially, there are 7 solar-powered bulbs in "1111110001". Only the 8th bulb (the middle '0') can be replaced with a solar-powered bulb. Replacing either of the other two '0' positions would place a new '1' directly adjacent to an existing '1', which violates the rule. After replacing 1 bulb, the maximum total of 8 solar-powered bulbs is achieved.

Constraints

  • lights consists only of the characters '0' and '1'.
  • 1 <= lights.length <= 10^5

More Amazon problems

drafts saved locally
public int IncreaseSolarPoweredBulbs(String lights) {
  // write your code here
}
lights"1111110001"
expected8
checking account