Task: Maxcounters It is an individual impression.
①Speed N*M
def solution(N, A):
    anslist = [0] * N
    maxvalue = 0
    for i in A:
        try:
            anslist[i-1] += 1
        except:
            maxvalue = max(anslist)
            anslist = [maxvalue] * N
    return anslist
I thought that if I did max in every try, it would be N * 2, so I tried to max only when necessary. But, of course, it depends on the frequency of appearance of N + 1, so it will be N * M. Convinced.
②Speed: N+M
def solution(N, A):
    anslist = [0] * N
    maxchecker = 0
    for i in A:
        try:
            anslist[i-1] += 1
            if anslist[i-1] > maxchecker:
                maxchecker = anslist[i-1]
        except:
            anslist = [maxchecker] * N
    return anslist
Impressions and learning: I don't know why N ** + M ** ??? .. .. Suspect. And I haven't reached speed N or Log (N).
Recommended Posts