Skip to content

Commit c59b6b2

Browse files
committedAug 31, 2023
31. Min. Number of Taps to Open to Water a Garden
1 parent 7cd7ed1 commit c59b6b2

File tree

2 files changed

+96
-28
lines changed

2 files changed

+96
-28
lines changed
 
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
## 31. Minimum Number of Taps to Open to Water a Garden
2+
3+
4+
The problem can be found at the following link: [Question Link](https://leetcode.com/problems/minimum-number-of-taps-to-open-to-water-a-garden/description/)
5+
6+
7+
### My Approach
8+
9+
This is a question on greedy approch, as we have given how to find the range of the given taps . We can calculate the range of each tap and check if we can manage to find a tap which have largest current range. we can do it as :
10+
11+
1. Initialize variables:
12+
- `minRange` and `maxRange` to keep track of the minimum and maximum positions that can be covered by the currently selected taps (both initially set to 0).
13+
- `taps` to keep track of the number of taps used (initialized to 0).
14+
- `curindex` to keep track of the index of the last selected tap (initialized to 0).
15+
16+
2. Create a while loop that continues until `maxRange` is greater than or equal to `n`. This loop is used to extend the covered range step by step.
17+
18+
3. Inside the while loop, iterate through the taps (represented by the `ranges` vector) starting from the `curindex`. For each tap `i`:
19+
- Check if the tap can cover the current position (`i - ranges[i]`) and if it can extend the range further (`i + ranges[i] > maxRange`).
20+
- If both conditions are met, update `maxRange` to `i + ranges[i]` and update `curindex` to `i`. This tap is chosen as it extends the maximum coverage.
21+
22+
4. After examining all taps within the current range, if `minRange` is equal to `maxRange`, it means that no tap can be selected to extend the coverage further. In this case, return -1 to indicate that it's impossible to cover the entire range.
23+
24+
5. Increment `taps` to count the tap that has been used to extend the coverage.
25+
26+
6. Update `minRange` to `maxRange` to indicate that the current range has been fully covered.
27+
28+
7. Repeat steps 3-6 until `maxRange` is greater than or equal to `n`.
29+
30+
31+
### Time and Auxiliary Space Complexity
32+
33+
- **Time Complexity**: `O(n)`
34+
- **Auxiliary Space Complexity**: `O(1)` because we used an extra queue..
35+
36+
37+
### Code (C++)
38+
39+
```cpp
40+
class Solution {
41+
public:
42+
int minTaps(int n, vector<int>& ranges) {
43+
int minRange =0, maxRange =0;
44+
int taps =0; int curindex =0;
45+
46+
while(maxRange < n){
47+
for(int i=curindex;i<ranges.size();i++){
48+
if(i-ranges[i]<=minRange && i+ranges[i]>maxRange){
49+
maxRange = i+ranges[i];
50+
curindex = i;
51+
}
52+
}
53+
if(minRange == maxRange) return -1;
54+
taps++;
55+
minRange = maxRange;
56+
}
57+
return taps;
58+
}
59+
};
60+
```
61+
62+
### Contribution and Support
63+
64+
For discussions, questions, or doubts related to this solution, please visit our [discussion section](https://leetcode.com/discuss/general-discussion). We welcome your input and aim to foster a collaborative learning environment.
65+
66+
If you find this solution helpful, consider supporting us by giving a `⭐ star` to the [rishabhv12/Daily-Leetcode-Solution](https://github.com/rishabhv12/Daily-Leetcode-Solution) repository.

‎README.md

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
1-
## 30. Minimum Replacements to Sort the Array
1+
## 31. Minimum Number of Taps to Open to Water a Garden
22

3-
The problem can be found at the following link: [Question Link](https://leetcode.com/problems/minimum-replacements-to-sort-the-array/description/)
3+
4+
The problem can be found at the following link: [Question Link](https://leetcode.com/problems/minimum-number-of-taps-to-open-to-water-a-garden/description/)
45

56

67
### My Approach
78

8-
The intution to solve this question is we can make all the element in the vector eqaul and also try to keep the replacement number as small as possible , we can achive this as :
9+
This is a question on greedy approch, as we have given how to find the range of the given taps . We can calculate the range of each tap and check if we can manage to find a tap which have largest current range. we can do it as :
910

1011
1. Initialize variables:
11-
- `curmax` to keep track of the current maximum value (initialized to `INT_MAX`).
12-
- `ans` to keep track of the minimum number of replacements (initialized to 0).
12+
- `minRange` and `maxRange` to keep track of the minimum and maximum positions that can be covered by the currently selected taps (both initially set to 0).
13+
- `taps` to keep track of the number of taps used (initialized to 0).
14+
- `curindex` to keep track of the index of the last selected tap (initialized to 0).
15+
16+
2. Create a while loop that continues until `maxRange` is greater than or equal to `n`. This loop is used to extend the covered range step by step.
1317

14-
2. Start a loop from the last element (`i` initialized to `n-1`) and move backwards towards the first element.
18+
3. Inside the while loop, iterate through the taps (represented by the `ranges` vector) starting from the `curindex`. For each tap `i`:
19+
- Check if the tap can cover the current position (`i - ranges[i]`) and if it can extend the range further (`i + ranges[i] > maxRange`).
20+
- If both conditions are met, update `maxRange` to `i + ranges[i]` and update `curindex` to `i`. This tap is chosen as it extends the maximum coverage.
1521

16-
3. Inside the loop, check if `nums[i]` is less than or equal to `curmax`. If it is, update `curmax` to `nums[i]`. This step ensures that `curmax` always represents the maximum value encountered so far.
22+
4. After examining all taps within the current range, if `minRange` is equal to `maxRange`, it means that no tap can be selected to extend the coverage further. In this case, return -1 to indicate that it's impossible to cover the entire range.
1723

18-
4. If `nums[i]` is greater than `curmax`, it means a replacement is needed to make them equal. Calculate the number of replacements required as follows:
19-
- If `nums[i]` is divisible by `curmax`, subtract 1 from the result and add it to `ans`.
20-
- Otherwise, divide `nums[i]` by `curmax` to get `div`, then add `div` to `ans`. Update `curmax` by dividing `nums[i]` by `div`, which ensures that `curmax` represents the maximum value encountered after the replacement.
24+
5. Increment `taps` to count the tap that has been used to extend the coverage.
2125

22-
5. Repeat steps 3-4 for all elements in the vector from right to left.
26+
6. Update `minRange` to `maxRange` to indicate that the current range has been fully covered.
27+
28+
7. Repeat steps 3-6 until `maxRange` is greater than or equal to `n`.
2329

2430

2531
### Time and Auxiliary Space Complexity
@@ -33,26 +39,22 @@ The intution to solve this question is we can make all the element in the vector
3339
```cpp
3440
class Solution {
3541
public:
36-
long long minimumReplacement(vector<int>& nums) {
37-
int n=nums.size();
38-
int curmax=INT_MAX;
39-
long long ans=0;
40-
41-
for(int i=n-1; i>=0; i--){
42-
if(nums[i]<=curmax){
43-
curmax=nums[i];
44-
}
45-
else{
46-
if(nums[i]%curmax==0){
47-
ans+=((nums[i]/curmax)-1);
48-
}else{
49-
ans+=((nums[i]/curmax));
50-
int div=(nums[i]/curmax)+1;
51-
curmax=nums[i]/div;
42+
int minTaps(int n, vector<int>& ranges) {
43+
int minRange =0, maxRange =0;
44+
int taps =0; int curindex =0;
45+
46+
while(maxRange < n){
47+
for(int i=curindex;i<ranges.size();i++){
48+
if(i-ranges[i]<=minRange && i+ranges[i]>maxRange){
49+
maxRange = i+ranges[i];
50+
curindex = i;
5251
}
5352
}
53+
if(minRange == maxRange) return -1;
54+
taps++;
55+
minRange = maxRange;
5456
}
55-
return ans;
57+
return taps;
5658
}
5759
};
5860
```

0 commit comments

Comments
 (0)