TopCoder Dynamic Programming Bad Neighbours Donation Collection Problem
Problem
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 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.
Constraints
- donations contains between 2 and 40 elements, inclusive.
- Each element in donations is between 1 and 1000, inclusive.
Examples
{ 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.
{ 11, 15 }
Returns: 15
{ 7, 7, 7, 7, 7, 7, 7 }
Returns: 21
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.
Constraints
- donations contains between 2 and 40 elements, inclusive.
- Each element in donations is between 1 and 1000, inclusive.
Examples
{ 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.
{ 11, 15 }
Returns: 15
{ 7, 7, 7, 7, 7, 7, 7 }
Returns: 21
Solution
int badNeighbours(int *a, int size) {
if(size == 2) {
int sum = findMax(a[0], a[1]);
return sum;
}
int i, j;
int *s = (int *)malloc(sizeof(int)*size);
int *b = (int *)malloc(sizeof(int)*size);
s[0]=a[0];
s[1]=a[1];
b[0]=1;
b[1]=0;
for(i=2;i<size;i++) {
s[i]=0;
b[i]=0;
}
for(i=2;i<size;i++) {
for(j=0;j<i-1;j++) {
if(s[i]<s[j]+a[i]) {
s[i]=s[j]+a[i];
b[i]=b[j];
}
}
}
if(b[size-1]==0) {
return s[size-1];
} else {
int min = findMin(a[0], a[size-1]);
int sum = findMax(s[size-2], s[size-1]-min);
return sum;
}
return 0;
}
This comment has been removed by the author.
ReplyDeleteGoibibo _Customer Care Helpline number _
Delete7063539605
Any problem call my agent (24*7) hours available.
Head office_
Contact hair_9958429949
Customer care Helpline number _
Any-problem call
___8670530538
Online problem call my assistant
___
______
Customer Care Helpline number _
7063539605
Any problem call my agent (24*7) hours available.
Head office_
Contact hair_9958429949
Customer care Helpline number _
Any-problem call
___8670530538
Online problem call my assistant
___
______
Customer Care Helpline number _
7063539605
Any problem call my agent (24*7) hours available.
Head office_
Contact hair_9958429949
Customer care Helpline number _
Any-problem call
___8670530538
Online problem call my assistant
___
______
Customer Care Helpline number _
7063539605
Any problem call my agent (24*7) hours available.
Head office_
Contact hair_9958429949
Customer care Helpline number _
Any-problem call
___8670530538
Online problem call my assistant
___
______
Customer Care Helpline number _
7063539605
Any problem call my agent (24*7) hours available.
Head office_
Contact hair_9958429949
Customer care Helpline number _
Any-problem call
___8670530538
Online problem call my assistant
___
______
Customer Care Helpline number _
7063539605
Any problem call my agent (24*7) hours available.
Head office_
Contact hair_9958429949
Customer care Helpline number _
Any-problem call
___8670530538
Online problem call my assistant
___
______
Customer Care Helpline number _
7063539605
Any problem call my agent (24*7) hours available.
Head office_
Contact hair_9958429949
Customer care Helpline number _
Any-problem call
___8670530538
Online problem call my assistant
___
______
Customer Care Helpline number _
7063539605
Any problem call my agent (24*7) hours available.
Head office_
Contact hair_9958429949
Customer care Helpline number _
Any-problem call
___8670530538
Online problem call my assistant
___
______
Customer Care Helpline number _
7063539605
Any problem call my agent (24*7) hours available.
Head office_
Contact hair_9958429949
Customer care Helpline number _
Any-problem call
___8670530538
Online problem call my assistant
___
______
Customer Care Helpline number _
7063539605
Any problem call my agent (24*7) hours available.
Head office_
Contact hair_9958429949
Customer care Helpline number _
Any-problem call
___8670530538
Online problem call my assistant
___
______
Customer Care Helpline number _
7063539605
Any problem call my agent (24*7) hours available.
Head office_
Contact hair_9958429949
Customer care Helpline number _
Any-problem call
___8670530538
Online problem call my assistant
___
______
Customer Care Helpline number _
7063539605
Any problem call my agent (24*7) hours available.
Head office_
Contact hair_9958429949
Customer care Helpline number _
Any-problem call
___8670530538
Online problem call my assistant
___
______
Goibibo _Customer Care Helpline number _
Delete7063539605
Any problem call my agent (24*7) hours available.
Head office_
Contact hair_9958429949
Customer care Helpline number _
Any-problem call
___8670530538
Online problem call my assistant
___
______
Customer Care Helpline number _
7063539605
Any problem call my agent (24*7) hours available.
Head office_
Contact hair_9958429949
Customer care Helpline number _
Any-problem call
___8670530538
Online problem call my assistant
___
______
Customer Care Helpline number _
7063539605
Any problem call my agent (24*7) hours available.
Head office_
Contact hair_9958429949
Customer care Helpline number _
Any-problem call
___8670530538
Online problem call my assistant
___
______
Customer Care Helpline number _
7063539605
Any problem call my agent (24*7) hours available.
Head office_
Contact hair_9958429949
Customer care Helpline number _
Any-problem call
___8670530538
Online problem call my assistant
___
______
Customer Care Helpline number _
7063539605
Any problem call my agent (24*7) hours available.
Head office_
Contact hair_9958429949
Customer care Helpline number _
Any-problem call
___8670530538
Online problem call my assistant
___
______
Customer Care Helpline number _
7063539605
Any problem call my agent (24*7) hours available.
Head office_
Contact hair_9958429949
Customer care Helpline number _
Any-problem call
___8670530538
Online problem call my assistant
___
______
Customer Care Helpline number _
7063539605
Any problem call my agent (24*7) hours available.
Head office_
Contact hair_9958429949
Customer care Helpline number _
Any-problem call
___8670530538
Online problem call my assistant
___
______
Customer Care Helpline number _
7063539605
Any problem call my agent (24*7) hours available.
Head office_
Contact hair_9958429949
Customer care Helpline number _
Any-problem call
___8670530538
Online problem call my assistant
___
______
Customer Care Helpline number _
7063539605
Any problem call my agent (24*7) hours available.
Head office_
Contact hair_9958429949
Customer care Helpline number _
Any-problem call
___8670530538
Online problem call my assistant
___
______
Customer Care Helpline number _
7063539605
Any problem call my agent (24*7) hours available.
Head office_
Contact hair_9958429949
Customer care Helpline number _
Any-problem call
___8670530538
Online problem call my assistant
___
______
Customer Care Helpline number _
7063539605
Any problem call my agent (24*7) hours available.
Head office_
Contact hair_9958429949
Customer care Helpline number _
Any-problem call
___8670530538
Online problem call my assistant
___
______
Customer Care Helpline number _
7063539605
Any problem call my agent (24*7) hours available.
Head office_
Contact hair_9958429949
Customer care Helpline number _
Any-problem call
___8670530538
Online problem call my assistant
___
______
Hey, this program is the easiest I found on the internet. Good Job!!
ReplyDeleteIt seems your solution doesn't work for {1, 100, 1, 100, 1}
ReplyDelete