Skip to content

Instantly share code, notes, and snippets.

@KPCCoiL
Created June 28, 2014 06:57
Show Gist options
  • Save KPCCoiL/eb3c275bdd4bf3658ca2 to your computer and use it in GitHub Desktop.
Save KPCCoiL/eb3c275bdd4bf3658ca2 to your computer and use it in GitHub Desktop.
#include <algorithm>
#include <vector>
using namespace std;
class BuildingHeights {
public:
int minimum(vector <int> heights) {
const int N = heights.size();
sort(heights.begin(),heights.end());
vector<int> accum(N);
accum[0] = heights[0];
for (int i = 1; i < N; i++)
accum[i] = accum[i-1]+heights[i];
int ans = 0;
for (int i = 2; i <= N; i++) {
int now = 1e9;
for (int j = 0; i+j-1 < N; j++)
now = min(now,heights[j+i-1]*i-(accum[j+i-1]-(j?accum[j-1]:0)));
ans ^= now;
}
return ans;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment