Last active
February 7, 2021 12:40
-
-
Save inspirit941/cb44f850357580786e5ae4d7b293cdbd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from copy import deepcopy | |
from itertools import combinations | |
import bisect | |
def solution(info, query): | |
answer = [] | |
table = dict() | |
tmp = [ | |
["cpp","java","python","-"], | |
['backend','frontend','-'], | |
['junior','senior','-'], | |
['chicken','pizza','-'] | |
] | |
# 모든 조건별 경우의 수 생성하기 | |
def make_table(cnt, string): | |
nonlocal table | |
if cnt == 4: | |
table[" and ".join(string)] = [] | |
return | |
for name in tmp[cnt]: | |
string.append(name) | |
make_table(cnt+1, string) | |
string.pop() | |
# 모든 조건을 담은 table 생성 | |
make_table(0, []) | |
for each_data in info: | |
data, score = each_data.split()[:-1], each_data.split()[-1] | |
score = int(score) | |
# - 가 들어갈 수 있는 공간 찾기 | |
for i in range(5): | |
candidates = combinations(range(4),i) | |
for position in candidates: | |
# Deepcopy로 값을 복사한 새 리스트 생성하기 | |
copy_data = deepcopy(data) | |
# - 가 들어갈 수 있는 idx에 "-" 입력 | |
for idx in position: | |
copy_data[idx] = "-" | |
# 조건에 해당하는 값 입력 - bisect 활용해 이진탐색이 가능하도록 정렬된 채 저장 | |
bisect.insort_left(table[" and ".join(copy_data)], score) | |
for q in query: | |
# 맨 뒷 숫자를 제외한 조건절 / 숫자 | |
q, score = q.rsplit(maxsplit = 1)[:-1][0], int(q.rsplit(maxsplit = 1)[-1]) | |
# bisect_left로, 동일한 값이 존재할 경우 count에 포함한다. | |
idx = bisect.bisect_left(table[q], int(score)) | |
answer.append(len(table[q])-idx) | |
return answer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment