Minimum amount of lamps needed to be installed

Given a string str containing only dots and asterisk. A dot represents free spaces and * represents lamps. A lamp at position i can spread its light at locations i-1, i and i+1. Determine the minimum number of lamps needed to illuminate the whole string.

Examples:

Input: str = “……”
Output: 2
There are initially no lamps so the whole string is in dark.We will install lamps at position 2 and 5.The lamp at position 2 will illuminate 1, 2, 3 and lamp at position 5 will illuminate 4, 5, 6 thus the whole string is illuminated.



Input: str = “*.*”
Output: 0

Approach: If we don’t have any asterisk * then for each 3 dots we need one lamp, so the answer is ceil(D/3) where D is the number of dots. The problem can be solved by creating a copy of the given string, and for each asterisk in the first string we place an asterisk at it’s adjacent indices in the second string.
So if the given string is “…**..” then the second string will be “..****.”.
After that, we count the number of dots in each block of consecutive dots and find the number of needed lamps for that block, For each block the answer will be ceil(D/3) and the total sum of these lamps will be the answer for the complete string.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the above approach
import math
  
# Function to print minimum amount 
# of lamps needed to be installed
def check(n, s):
  
    # Create the modified string with 
    # v[i-1] = v[i + 1] = * where s[i] = *
    v = [""] * n
    for i in range(n):
        if (s[i] == "*"):
            v[i] = "*"
  
            # Checking valid index and then replacing 
            # "." with "*" on the surrounding of a * 
            if (i > 0 and i < n - 1):
                v[i + 1] = "*"
                v[i - 1] = "*"
            if (i == 0 and n != 1):
                v[i + 1] = "*"
            if (i == n - 1 and n != 1):
                v[i - 1] = "*"
        else:
  
            # Just copying if the character is a "."
            if (v[i] != "*"):
                v[i] = "."
  
    # Creating the string with the list v
    xx = ''.join(v)
  
    # Splitting the string into blocks 
    # with "*" as delimiter
    x = xx.split("*")
    s = 0
    for i in range(len(x)):
  
        # Continuing if the string length is 0
        if (x[i] == ""):
            continue
  
        # Adding number of lamps for each block of "."
        s += math.ceil(len(x[i]) / 3)
    print(s)
  
# Driver code
s = "......"
n = len(s)
check(n, s)

chevron_right


Output:

2


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.