Skip to content

Commit

Permalink
Added 1326
Browse files Browse the repository at this point in the history
  • Loading branch information
MonitSharma authored Aug 31, 2023
1 parent 25c0011 commit 032e84a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
23 changes: 23 additions & 0 deletions 1326-Minimum number of taps to open to water a garden/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
int minTaps(int n, vector<int>& ranges) {
vector<int> arr(n + 1, 0);
for(int i = 0; i < ranges.size(); ++i) {
if(ranges[i] == 0) continue;
int left = max(0, i - ranges[i]);
arr[left] = max(arr[left], i + ranges[i]);
}

int end = 0, far_can_reach = 0, cnt = 0;
for(int i = 0; i <= n; ++i) {
if(i > end) {
if(far_can_reach <= end) return -1;
end = far_can_reach;
++cnt;
}
far_can_reach = max(far_can_reach, arr[i]);
}

return cnt + (end < n);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
public int minTaps(int n, int[] ranges) {
int[] nums = new int[n + 1];

for (int i = 0; i <= n; ++i) {
int l = Math.max(0, i - ranges[i]);
int r = Math.min(n, i + ranges[i]);
nums[l] = Math.max(nums[l], r - l);
}

int ans = 0;
int end = 0;
int farthest = 0;

for (int i = 0; i < n; i++) {
farthest = Math.max(farthest, i + nums[i]);
if (i == end) {
++ans;
end = farthest;
}
}

return end == n ? ans : -1;
}
}
20 changes: 20 additions & 0 deletions 1326-Minimum number of taps to open to water a garden/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
nums = [0] * (n + 1)

for i, range_ in enumerate(ranges):
l = max(0, i - range_)
r = min(n, range_ + i)
nums[l] = max(nums[l], r - l)

ans = 0
end = 0
farthest = 0

for i in range(n):
farthest = max(farthest, i + nums[i])
if i == end:
ans += 1
end = farthest

return ans if end == n else -1
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ This repository contains solutions to over 400 Leetcode problems that are organi
| 1232 | Check If It Is a Straight Line | [![Medium](https://img.shields.io/badge/Medium-12100E?style=for-the-badge&logo=medium&logoColor=white)](https://medium.com/@_monitsharma/daily-leetcode-problems-problem-1232-check-if-it-is-a-straight-line-bf34f9460302) |
| 1312 | Minimum Insertion Steps to Make a String Palindrome | [![Medium](https://img.shields.io/badge/Medium-12100E?style=for-the-badge&logo=medium&logoColor=white)](https://medium.com/@_monitsharma/daily-leetcode-problems-problem-1312-minimum-insertion-steps-to-make-a-string-palindrome-1f639f96ad68) |
| 1318 | Minimum Flips to Make a OR b Equal to c | [![Medium](https://img.shields.io/badge/Medium-12100E?style=for-the-badge&logo=medium&logoColor=white)](https://medium.com/@_monitsharma/daily-leetcode-problems-problem-1318-minimum-flips-to-make-a-or-b-equal-to-c-6690d91c0e47) |
| 1326 | Minimum Number of Taps to Open to Water a Garden | [![Medium](https://img.shields.io/badge/Medium-12100E?style=for-the-badge&logo=medium&logoColor=white)](https://medium.com/@_monitsharma/daily-leetcode-problems-problem-1326-minimum-number-of-taps-to-open-to-water-a-garden-83bb886e54c0) |
| 1351 | Count Negative Numbers in a Sorted Matrix | [![Medium](https://img.shields.io/badge/Medium-12100E?style=for-the-badge&logo=medium&logoColor=white)](https://medium.com/@_monitsharma/daily-leetcode-problems-problem-1351-count-negative-numbers-in-a-sorted-matrix-13b7bb33487e) |
| 1372 | Longest ZigZag Path in a Binary Tree | [![Medium](https://img.shields.io/badge/Medium-12100E?style=for-the-badge&logo=medium&logoColor=white)](https://medium.com/@_monitsharma/daily-leetcode-problems-problem-1372-longest-zigzag-path-in-a-binary-tree-8cc04cadfe21) |
| 1376 | Time needed to inform all employees | [![Medium](https://img.shields.io/badge/Medium-12100E?style=for-the-badge&logo=medium&logoColor=white)](https://medium.com/@_monitsharma/daily-leetcode-problems-problem-1376-time-needed-to-inform-all-employees-2787441bc80c) |
Expand Down

0 comments on commit 032e84a

Please sign in to comment.