728x90

나의 풀이
from collections import defaultdict
from itertools import combinations
def solution(info, query):
answer = []
pool = defaultdict(list)
for i in info:
w = i.split(' ')
for n in range(0, 5):
for x in combinations(w[:-1], n):
pool[x].append(int(w[-1]))
for x in pool.values():
x.sort()
for q in query:
w = q.replace('and', '').replace('-', '').split()
cut = int(w[-1])
x = tuple(w[:-1])
left, right = 0, len(pool[x])
mid = (left + right) // 2
if pool[x]:
while left < right:
if pool[x][mid] >= cut:
if mid == 0 or pool[x][mid - 1] < cut:
break
right = mid - 1
else:
left = mid + 1
mid = (left + right) // 2
answer.append(len(pool[x]) - mid)
return answer
- bisect 라이브러리를 이용하면 이분탐색을 더 간단하게 할 수 있었을 것.
반응형
'Coding Test > Programmers' 카테고리의 다른 글
| [프로그래머스/파이썬] 괄호 회전하기 (0) | 2022.06.16 |
|---|---|
| [프로그래머스/파이썬] 후보키 (0) | 2022.06.16 |
| [프로그래머스/파이썬] 예상 대진표 (0) | 2022.06.13 |
| [프로그래머스/파이썬] 게임 맵 최단거리 (0) | 2022.06.13 |
| [프로그래머스/파이썬] 빛의 경로 사이클 (0) | 2022.06.13 |