Algorithm: Max Counters

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

    The key is that [0] * N is an N operation. If that exists in a for loop it will become N*M. Tested in Codility 100%

                # you can write to stdout for debugging purposes, e.g.
                # print "this is a debug message"
    
                def solution(N, A):
                    # write your code in Python 2.7
                    count = [0] * N
                    maxCounter = 0
                    minCounter = 0
                    for x in A:
                        if x <= N and x >= 1:
                            count[x-1] = max(count[x-1], minCounter) + 1
                            if maxCounter < count[x-1]:
                                maxCounter = count[x-1]
                        if x == N + 1:
                            minCounter = maxCounter                
                    for i in xrange(N):
                        count[i] = max(count[i], minValue)
                    return count
    

提交回复
热议问题