1326. Minimum Number of Taps to Open to Water a Garden
Contents
https://leetcode.com/problems/minimum-number-of-taps-to-open-to-water-a-garden/
class Solution {
public:
int minTaps(int n, vector<int>& ranges) {
vector<int> dp(n + 1, 0x3f3f3f3f);
dp[0] = 0;
for(int i = 0; i < ranges.size(); ++i) {
int l = max(0, i - ranges[i]);
int r = min(n, i + ranges[i]);
for(int j = l; j <= r; ++j) {
dp[j] = min(dp[j], dp[l] + 1);
}
}
int ans = 0;
for(int i = 0; i <= n; ++i) {
if(dp[i] == 0x3f3f3f3f) {
return -1;
}
ans = max(ans, dp[i]);
}
return ans;
}
};