You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## 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..
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.
Copy file name to clipboardExpand all lines: README.md
+30-28Lines changed: 30 additions & 28 deletions
Original file line number
Diff line number
Diff 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
2
2
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/)
4
5
5
6
6
7
### My Approach
7
8
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 :
9
10
10
11
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.
13
17
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.
15
21
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.
17
23
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.
21
25
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`.
23
29
24
30
25
31
### 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
0 commit comments