Algorithm: Max Counters

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

    def solution(N, A):
        res = [0] * N
        maxV, minV = 0, 0
        for x in A:
            if 1 <= x <= N:
                res[x-1] = max(res[x-1], minV) + 1
                maxV = max(maxV, res[x-1])
            else: minV = maxV
        for i in range(N):
            res[i] = max(res[i], minV)
        return res
    

提交回复
热议问题