forked from keshavnandan/Topcoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BadNeighbors.txt
63 lines (41 loc) · 1.73 KB
/
BadNeighbors.txt
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
56
57
58
59
60
61
62
63
PROBLEM STATEMENT
The old song declares "Go ahead and hate your neighbor", and the residents of
Onetinville have taken those words to heart. Every resident hates his next-door
neighbors on both sides. Nobody is willing to live farther away from the town's
well than his neighbors, so the town has been arranged in a big circle around
the well. Unfortunately, the town's well is in disrepair and needs to be restored. You
have been hired to collect donations for the Save Our Well fund.
Each of the town's residents is willing to donate a certain amount, as specified in the vector <int>
donations, which is listed in clockwise order around the well. However, nobody is willing to
contribute to a fund to which his neighbor has also contributed. Next-door neighbors are always listed
consecutively in donations, except that the first and last entries in donations are also
for next-door neighbors. You must calculate and return the maximum amount of donations that can be collected.
DEFINITION
Class:BadNeighbors
Method:maxDonations
Parameters:vector <int>
Returns:int
Method signature:int maxDonations(vector <int> donations)
CONSTRAINTS
-donations contains between 2 and 40 elements, inclusive.
-Each element in donations is between 1 and 1000, inclusive.
EXAMPLES
0)
{ 10, 3, 2, 5, 7, 8 }
Returns: 19
The maximum donation is 19, achieved by 10+2+7. It would be better to take 10+5+8 except that
the 10 and 8 donations are from neighbors.
1)
{ 11, 15 }
Returns: 15
2)
{ 7, 7, 7, 7, 7, 7, 7 }
Returns: 21
3)
{ 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 }
Returns: 16
4)
{ 94, 40, 49, 65, 21, 21, 106, 80, 92, 81, 679, 4, 61,
6, 237, 12, 72, 74, 29, 95, 265, 35, 47, 1, 61, 397,
52, 72, 37, 51, 1, 81, 45, 435, 7, 36, 57, 86, 81, 72 }
Returns: 2926