Codility/4.2_maxCounters.js
// Should not update maxCounter inside the first loop
Should not update maxCounter inside the first loop
Should not update maxCounter inside the first loop
function solution(N, A) {
  let counter = Array(N).fill(0);
  let maxValue = 0, maxCounter = 0;
  for(let i = 0; i < A.length; i++) {
    if(A[i] === N + 1) {
      maxCounter = maxValue;
    } else if(A[i] <= N) {
      counter[A[i]-1] = Math.max(counter[A[i]-1], maxCounter);
      counter[A[i]-1]++;
      maxValue = Math.max(counter[A[i]-1], maxValue); 
    }
  }
  return counter.map((value) => Math.max(value, maxCounter));
}
function solution ( N , A ) { let counter = Array ( N ) . fill ( 0 ) ; let maxValue = 0 , maxCounter = 0 ; for ( let i = 0 ; i < A . length ; i ++ ) { if ( A [ i ] === N + 1 ) { maxCounter = maxValue ; } else if ( A [ i ] <= N ) { counter [ A [ i ] - 1 ] = Math . max ( counter [ A [ i ] - 1 ] , maxCounter ) ; counter [ A [ i ] - 1 ] ++ ; maxValue = Math . max ( counter [ A [ i ] - 1 ] , maxValue ) ; } } return counter . map ( ( value ) => Math . max ( value , maxCounter ) ) ; }
Should not update maxCounter inside the first loop
Should not update maxCounter inside the first loop
// first loop  // Memorize maxValue // console.log(counter, maxCounter) // Update maxCounter before return