Algorithm: Max Counters

后端 未结 22 1605
孤城傲影
孤城傲影 2021-02-04 07:40

I have the following problem:

You are given N counters, initially set to 0, and you have two possible operations on them:

  • increase(X) − counter X is increa
22条回答
  •  醉梦人生
    2021-02-04 08:36

    with js max score that I can get is 77%

    any improvement?

    function solution(N, A) {
    
      let counters = [];
    
      //fill counter with 0
      for (let i = 0; i < N; i += 1) {
        counters[i] = 0;
      }
    
      //loop array and set counters
      for (let i = 0; i < A.length; i += 1) {
    
        //0 index fix
        let position = A[i] - 1;
    
        if (A[i] <= N) {
          counters[position] += 1;
        } else {
          let max = Math.max(...counters);
          counters.fill(max)
        }
    
      }
    
      return counters;
    
    }
    

提交回复
热议问题