Algorithm: Max Counters

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

    def solution(N, A):
        # write your code in Python 2.6
        res = [0] * N
        m = 0
        minValue = 0
        for x in A:
            if 1 <= x <= N:
                res[x - 1] = max(res[x - 1], minValue) + 1
                if res[x - 1] > m:
                    m = res[x - 1]
            else:
                minValue = m
        for i in xrange(N):
            res[i] = max(res[i], minValue)
        return res
    

提交回复
热议问题