Skip to content

Instantly share code, notes, and snippets.

@alireza-mpr
Created November 13, 2020 07:01
Show Gist options
  • Save alireza-mpr/84ff881e573abe385e945b32aecfa3d1 to your computer and use it in GitHub Desktop.
Save alireza-mpr/84ff881e573abe385e945b32aecfa3d1 to your computer and use it in GitHub Desktop.
function solution(N, A) {
var counters = new Array(N);
var max = 0;
var toBeAppliedMax = 0;
for (let K = 0; K < A.length; K++) {
const op = A[K];
if (op == N + 1) {
toBeAppliedMax = max;
}
if (op >= 1 && op <= N) {
const counterInx = op - 1;
let currVal = counters[counterInx] || 0;
if (currVal < toBeAppliedMax)
currVal = toBeAppliedMax;
currVal++;
counters[counterInx] = currVal;
if (max < currVal) max = currVal;
}
}
counters = [...counters].map(p => Math.max((p || 0), toBeAppliedMax));
return counters;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment