-
Notifications
You must be signed in to change notification settings - Fork 985
/
Copy pathminimumNumberOfFountainsToCoverGarden.cpp
55 lines (51 loc) · 1.6 KB
/
minimumNumberOfFountainsToCoverGarden.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
There is a one-dimensional garden of starting at 0 and ending at n, with n + 1 fountains. In each position of the N length garden, a fountain has been installed.
Given an array a[]such that a[i] describes the coverage limit of ith fountain.
A fountain can cover the range from the position max(i – a[i], 1) to min(i + a[i], N).
In beginning, all the fountains are switched off.
The task is to find the minimum number of fountains needed to be activated such that the whole N-length garden can be covered by water.
*/
#include <iostream>
#include <vector>
using namespace std;
int minTaps(int n, vector<int>& range)
{
int answer = 1;
vector<int> dp(n + 1, 0);
for(int i = 0; i <= n; ++i)
{
int left = max(i - range[i], 0);
int right = min(i + range[i], n);
dp[left] = max(dp[left], right);
}
// Now this dp[] array formed turns into jump game ii problem of leetcode
int rightPossible = dp[0], maxRightNext = -1;
for(int i = 0; i <= n; ++i)
{
maxRightNext = max(maxRightNext, dp[i]);
if(i == rightPossible)
{
answer++;
rightPossible = maxRightNext;
}
if(rightPossible == n)
{
return answer;
}
}
return -1;
}
int main()
{
int n, answer;
cout << "Enter number of fountains" << endl;
cin >> n;
vector<int> range(n);
cout << "Enter coverage limit of all fountains" << endl;
for(int i = 0; i < n; ++i)
{
cin >> range[i];
}
answer = minTaps(n - 1, range);
cout << "Minimum number of fountains needed is " << answer << endl;
}