Skip to content

Instantly share code, notes, and snippets.

@ha2ne2
Created February 9, 2014 04:29
Show Gist options
  • Save ha2ne2/8894281 to your computer and use it in GitHub Desktop.
Save ha2ne2/8894281 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
using namespace std;
class BadNeighbors
{
public:
int maxDonations(vector <int> donations){
int tmp[2] = {0};
bool isEven = donations.size() % 2 == 0;
for (int i=0; i<donations.size(); i++) {
tmp[i%2]+=donations[i];
}
return isEven? max(tmp[0],tmp[1]):
max(max(tmp[0]-donations[0],
tmp[0]-donations[donations.size()-1]),
tmp[1]) ;
}
void test() {
cout << this->maxDonations({10,3,2,5,7,8}) << endl;
cout << this->maxDonations({7,7,7,7,7,7,7}) << endl;
cout << this->maxDonations({1,2,3,4,5}) << endl;
}
};
int main() {
BadNeighbors b;
b.test();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment