Each of the town's residents is willing to donate a certain amount, as
specified in the 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.
BadNeighbors
maxDonations
int[]
int
public int maxDonations(int[] donations)(be sure your method is public)
donations
contains between 2 and 40 elements, inclusive.
donations
is between 1 and 1000, inclusive.
{ 10, 3, 2, 5, 7, 8 } Returns: 19The 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.
{ 11, 15 } Returns: 15
{ 7, 7, 7, 7, 7, 7, 7 } Returns: 21
{ 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 } Returns: 16
{ 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