-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25c0011
commit 032e84a
Showing
4 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
1326-Minimum number of taps to open to water a garden/solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
25 changes: 25 additions & 0 deletions
25
1326-Minimum number of taps to open to water a garden/solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
1326-Minimum number of taps to open to water a garden/solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters