Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a python code to find the minimum number of water tanks needed to save the rainwater from all houses in a street. Today our

Write a python code to find the minimum number of water tanks needed to save the rainwater from all houses in a street.

Today our world is approaching an ecological crisis. Due to global warming, the sea level is rising. At the same time, the amount of drinkable water is decreasing. One idea about preventing the loss of drinkable water is the propagation of rainwater storage, i.e. equipping houses with a water tank for rainwater.

You are given a string S describing a street, in which 'H' denotes a house and '' denotes an empty plot. You may place water tanks in empty plots to collect rainwater from nearby houses. A house can collect its own rainwater if there is a tank next to it (on either the left or the right side).

Your task is to find the minimum number of water tanks needed to collect rainwater from all of the houses.

For example, given S = "HHH", you can collect rainwater from all three houses by using two water tanks. You can position one water tank between the first and second houses and the other after the third house. This placement of water tanks can be represented as "HTHHT", where 'T' denotes a water tank.

Write a function:

def solution(S)

that, given a string S of length N, returns the minimum number of water tanks needed.

If there is no solution, return -1.

Examples:

1. Given S = "HHH", the function should return 2, as explained above.

2. Given S = "H", the function should return -1. There is no available plot on which to place a water tank.

3. Given S = "HHHH", the function should return -1. There is only one plot to put a water tank, and it is impossible to collect rainwater from the first and last houses.

4. Given S = "HHHHH", the function should return 3. One possible way of placing water tanks is "HTHHTHTH".

Assume that:

N is an integer within the range [1..20];

string S consists only of the characters "-" and/or "H".

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Marketing The New Profit Frontier

Authors: Ed Burnett

1st Edition

0964535629, 978-0964535626

More Books

Students also viewed these Databases questions