Created
September 23, 2017 07:13
-
-
Save jrziviani/52b604e790857d4a737d152e0a31e4de to your computer and use it in GitHub Desktop.
This code solves the https://codility.com/demo/take-sample-test/number_solitaire/ - DP - HARD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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