Skip to content

Instantly share code, notes, and snippets.

@kexoth
Created March 18, 2015 00:15
Show Gist options
  • Save kexoth/bc13cd8d7c0684fa7989 to your computer and use it in GitHub Desktop.
Save kexoth/bc13cd8d7c0684fa7989 to your computer and use it in GitHub Desktop.
function solution(N, A) {
var counters = new Uint32Array(N),
max = 0,
gMax = 0;
for (i = 0; i < A.length; i++) {
var value = A[i];
if (value > N) {
max = gMax;
}
else {
var counter = counters[value-1];
if (counter < max) counter = max;
counters[value-1] = ++counter;
if (counter > gMax) {
gMax = counter;
}
}
}
for (i = 0; i < N; i++) {
if (counters[i] < max) counters[i] = max;
}
return counters;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment