0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Codility lesson time complexity と 自分の回答メモ (Python)

Last updated at Posted at 2020-11-03

#Task: Maxcounters
個人の感想です。

##①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

#####感想と学び
毎回tryの中でmaxやるとN2になってしまうと思ったので、
必要な時だけ限定してmaxすることを試みた。が、当然ながらN+1の登場頻度に依存してしまうのでN
Mになるよなと。納得。

##②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

感想と学び:なんでN +M ???わからない。。。要検討。そしてspeed N or Log(N)にたどり着けていない。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?