Algorithm: Max Counters

后端 未结 22 1568
孤城傲影
孤城傲影 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:20

    Ruby 100%

    
    def solution(n, a)
      max = 0
      offsets = a.inject(Hash.new(max)) do |acc, el|
        next Hash.new(max) if el == n+1
        acc[el] +=1
        max = acc[el] if max < acc[el]
        acc
      end
      (1..n).map{|i| offsets[i]}
    end
    

提交回复
热议问题