- 기본 반복문을 통해 구현을 하면 테케는 다 통과하지만 시간초과로 효율성이 0점이 나온다.
- 구글링을 참고하여 보니 딕셔너리 + 이분탐색을 사용해야 했다.(풀이 출처: https://hongcoding.tistory.com/56)
from itertools import combinations
from bisect import bisect_left
def solution(info, query):
answer = []
info_dict = {}
for i in range(len(info)):
infol = info[i].split()
mykey = infol[:-1]
myval = infol[-1]
for j in range(5):
for c in combinations(mykey, j):
tmp = ''.join(c)
if tmp in info_dict:
info_dict[tmp].append(int(myval))
else:
info_dict[tmp] = [int(myval)]
for k in info_dict:
info_dict[k].sort() # dict안의 조합들을 점수순으로 정렬
for qu in query: # query도 마찬가지로 key와 value로 분리
myqu = qu.split(' ')
qu_key = myqu[:-1]
qu_val = myqu[-1]
while 'and' in qu_key:
qu_key.remove('and')
while '-' in qu_key:
qu_key.remove('-')
qu_key = ''.join(qu_key)
if qu_key in info_dict: #query의 key가 info_dict의 key로 존재하면
scores = info_dict[qu_key]
if scores:
enter = bisect_left(scores, int(qu_val))
answer.append(len(scores)-enter)
else:
answer.append(0)
return answer
'알고리즘' 카테고리의 다른 글
[Gold V] 테트로미노 - 14500 (구현,브루트포스,dfs) (0) | 2022.07.07 |
---|---|
[Gold V] 로봇 청소기 - 14503(구현,그래프) (1) | 2022.07.06 |
[Gold V] 치킨 배달 - 15686(구현) (0) | 2022.07.04 |
[Silver III] 통계학 - 2108(수학,구현) (1) | 2022.07.03 |
[프로그래머스][level 2] k진수에서 소수 개수 구하기 - 92335 (0) | 2022.07.02 |