Water the plants | Practice | GeeksforGeeks

A gallery with plants is divided into n parts, numbered : 0,1,2,3...n-1. There are provisions for attaching water sprinklers at every partition. A sprinkler with range x at partition i can water all partitions from i-x to i+x.
Given an array gallery[ ] consisting of n integers, where gallery[i] is the range of sprinkler at partition i (power==-1 indicates no sprinkler attached), return the minimum number of sprinklers that need to be turned on to water the complete gallery.
If there is no possible way to water the full length using the given sprinklers, print -1.
Example 1:
Input:
n = 6
gallery[ ] = {-1, 2, 2, -1, 0, 0}
Output:
2
Explanation: Sprinklers at index 2 and 5
can water thefull gallery, span of
sprinkler at index 2 = [0,4] and span
â€‹of sprinkler at index 5 = [5,5].
Example 2:
Input:
n = 9
gallery[ ] = {2, 3, 4, -1, 2, 0, 0, -1, 0}
Output:
-1
Explanation: No sprinkler can throw water
at index 7. Hence all plants cannot be
watered.
Example 3:
Input:
n = 9
gallery[ ] = {2, 3, 4, -1, 0, 0, 0, 0, 0}
Output:
3
Explanation: Sprinkler at indexes 2, 7 and
8 together can water all plants.
Your task:
Your task is to complete the function min_sprinklers() which takes the array gallery[ ] and the integer n as input parameters and returns the value to be printed.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ n ≤ 105
gallery[i] ≤ 50
0
himanshujain4575 months ago
Time Complexity: O(NLOGN)
Space Complexity: O(1)
    int min_sprinklers(int gallery[], int n)   {      int mn=0,mx=0,ans=0,index=0,pos=0;      if(n==1 and gallery[0]==-1)      return -1;      while(mn<n)      {          pos=0;          for(int i=index;i<n;i++)          {              if( gallery[i]!=-1 and i-gallery[i]<=mn and i+gallery[i]>mx)              {                  mx=i+gallery[i];                  index=i;                  pos=1;                                }          }                   if(!pos)          return -1;          ans++;          mn=mx+1;      }      return ans;
0
yash76757goel6 months ago
class Solution{   public:   int min_sprinklers(int gallery[], int n)   {       // code here       vector<pair<int,int>>sprinklers;       for(int i=0;i<n;i++)       {           if(gallery[i]!=-1)           sprinklers.push_back({i-gallery[i],i+gallery[i]});       }       sort(sprinklers.begin(),sprinklers.end());       int target =0;       int ans=0,i=0;       while(target<n)       {           if(i==sprinklers.size()||sprinklers[i].first>target)           return -1;           int max_range = sprinklers[i].second;       while(i+1<sprinklers.size()&&sprinklers[i+1].first<=target)       {           i++;           max_range=max(max_range,sprinklers[i].second);       }       if(max_range<target)       return -1;       ans++;       target=max_range+1;       i++;       }       return ans;   }};
0
Kaushik Shah9 months ago
Kaushik Shah
could someone check this?i think the given output is wrong Input:
14-1 5 1 -1 5 -1 -1 5 0 2 -1 -1 2 0
Its Correct output is:
2
And Your Code's output is:
3
my code:https://practice.geeksforge...
0
Kaushik Shah
This comment was deleted.
+1
Sidharth Priyadarshi10 months ago
Sidharth Priyadarshi
/*Idea :- first create a range array of size n, initially all values as -1
- traverse all over the array    - take a sprinkler, find the maximum range of that sprikler        // let at index 3, sprikler value is 5, means this can water the plants range -2 to 8 units // take care of boundary conditions
    - if arr[i]==-1, i.e it can not give water to any plant         // continue    - now check if the maximum distance that this sprinkler can watered is greater than range[i] or not    - if yes,          - then update the range array from i-arr[i] to i+arr[i] with maximum distance i.e.  i+arr[i]        // this will indicate that a certain region is of plant sprinkled by a specific sprikler
 - now initialize count as 0 and j as 0  - traverse the range array until u reach last node    - check wheter range of j is -1   // means we have some plant which can not be watered        if yes , then return -1 // bcz all plants are not watered    otherwise        j will updated as range of j  // range of j contains the maximum distance of watering        increment count        increment j by 1  // bcz now we have to check from next place  // curr place is already sprinkled
    finally return count*/ https://uploads.disquscdn.c...
0
Shapnesh Singh Tiwari10 months ago
Shapnesh Singh Tiwari
Complete solution c++https://youtu.be/VVXjhup_NPM
0
Nihal Chaturvedi10 months ago
Nihal Chaturvedi
Approach -> We are setting our left range and trying to maximize our right range . Intution is similar to Minimum jump Problem .
Python Program https://uploads.disquscdn.c...
0
PALASH HIMANSHU RATHORE10 months ago
PALASH HIMANSHU RATHORE
This is leetcode hard problem why this is medium here?
0
Debashish kumar sahoo10 months ago
Debashish kumar sahoo
int min_sprinklers(int gallery[], int n)    {        // code here        int temp[n];        memset(temp,-1,sizeof(temp));        for(int i=0; i<n; i++){="" if(gallery[i]="=" 0){="" temp[i]="max(i," temp[i]);="" }else="" if(gallery[i]="" !="-1){" int="" end="i+gallery[i];" int="" start="max(0," i-gallery[i]);="" for(int="" x="start;" x="" <="min(n,end);" x++){="" temp[x]="max(end," temp[x]);="" }="" }="" }="" int="" ans="0," i="0" ;="" while(i="" <="" n){="" if(i="=" -1="" ||="" temp[i]="=" -1)="" return="" -1;="" ans++;="" i="temp[i]" +="" 1;="" }="" return="" ans;="" }<="" code="">
0
molla Baquar Abbas10 months ago
molla Baquar Abbas
simple solution in c++ using vectors and sorting!class Solution{    public:    int min_sprinklers(int gallery[], int n)    {        vector<vector<int>> sprinklers;        for(int i = 0; i < n;i++){            if(gallery[i] > -1){                sprinklers.push_back({i-gallery[i],i+gallery[i]});            }         }
        sort(sprinklers.begin(),sprinklers.end());
        int j = 0,target = 0;        int sprinklers_on = 0;        while(target < n){            if(j == sprinklers.size() || sprinklers[j][0] > target){                return -1;            }            int max_range = sprinklers[j][1];            while(j + 1 < sprinklers.size() && sprinklers[j+1][0] <= target){                j += 1;                max_range = max(max_range,sprinklers[j][1]);            }            if(max_range < target){                return -1;            }            sprinklers_on++;            target = max_range + 1;            j++;        }        return sprinklers_on;    }};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.