Created
November 13, 2020 07:01
-
-
Save alireza-mpr/84ff881e573abe385e945b32aecfa3d1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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