http://codility.com/demo/take-sample-test/maxcounters
简单题。注意要记录两个max,一个是最大值,一个是已经生效的最大值。
// you can also use includes, for example:
// #include <algorithm>
vector<int> solution(int N, vector<int> &A) {
// write your code in C++98
int max_val = 0;
int effective_max = 0;
vector<int> ans(N);
for (int i = 0; i < A.size(); i++) {
if (A[i] <= N) {
int idx = A[i] - 1;
if (ans[idx] < effective_max)
ans[idx] = effective_max;
ans[idx]++;
if (ans[idx] > max_val) {
max_val = ans[idx];
}
}
else {
effective_max = max_val;
}
}
for (int i = 0; i < N; i++) {
if (ans[i] < effective_max)
ans[i] = effective_max;
}
return ans;
}