18. Water the plants
Last updated
Last updated
The problem can be found at the following link: Question Link
I am using a greedy approach to find the minimum number of sprinklers needed to water all the plants, here is the approach.
For each plant, determine the range it can be watered. This is done by considering the plant's position and its sprinkler range.
Update a vector range
to store the maximum rightmost position each position can be watered.
Traverse the plants and count the number of sprinklers needed based on the updated range
vector.
Also check, If any position is not covered by any sprinkler, return -1.
Otherwise, count the number of sprinklers needed to water all the plants.
Time Complexity: The algorithm has a time complexity of O(N*max(gallery[i]))
, where N is the number of plants, and max(gallery[i]) is the range of water sprinkler.
Auxiliary Space Complexity: The algorithm uses O(N)
extra space to store the range
vector.
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the getlost01/gfg-potd repository.