Skip to content

Instantly share code, notes, and snippets.

@jrziviani
Created September 23, 2017 07:13
Show Gist options
  • Save jrziviani/52b604e790857d4a737d152e0a31e4de to your computer and use it in GitHub Desktop.
Save jrziviani/52b604e790857d4a737d152e0a31e4de to your computer and use it in GitHub Desktop.
#include <vector>
int solution(vector<int> &A) {
vector<int> st(A.size() + 1);
st[0] = A[0];
int last_max = 0;
for (size_t i = 1; i < A.size(); i++) {
st[i] = max(st[i - 1], st[last_max]) + A[i];
st[last_max] = max(st[last_max], st[i - 1]);
if (i - last_max >= 6) {
last_max++;
for (int j = last_max + 1, k = last_max + 6; j < k; j++) {
if (st[j] > st[last_max]) {
last_max = j;
}
}
}
}
return st[A.size() - 1];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment