728x90

< 기존 풀이 >

Counter 를 사용하지 않고 함수를 만들고 for 문을 만들어서 돌렸더니 역시 시간 초과가 발생했다. 

 

< 해결 방법 >

Counter 을 사용했지만 없는 숫자를 0 으로 출력하는 부분에서 막혔고, 다른 사람의 답을 보니 most_common 이 아닌 그냥 Counter 를 이용했더니 리스트 내 값에 따라 참조를 알아서 하는 것을 알게 되었다. most_common 은 튜플로 반환 해주지만 값 자체로 참조할 때는 Counter 가 더 효율적인 것 같다.

 

import sys
from collections import Counter
 
a = sys.stdin.readline()
 
b = list(map(int,sys.stdin.readline().rstrip().split(" ")))
a = sys.stdin.readline()    
c = list(map(int,sys.stdin.readline().rstrip().split(" ")))
 
b = Counter(b).most_common()
 
check = [ ]
 
for i in range(0,len(b)):
    check.append(b[i][0])
 
 
 
for i in range(0,len(c)):
 
    k = c[i]
 
    if k not in check:
        print(0,end = " ")
    else:
        for j in range(0,len(b)):
            if b[j][0]==k:
                print(b[j][1], end = " ")
                break
 
728x90

+ Recent posts