Coding Test/프로그래머스(Python)
[프로그래머스/Python] Level 2_순위 검색 (2021 KAKAO BLIND RECRUITMENT)
syunze
2023. 11. 12. 21:35
📌문제 유형
2021 KAKAO BLIND RECRUITMENT
📌문제
📌나의 문제풀이
- 정확성 통과, 효율성 4문제 오답
import re
def solution(info, query):
answer = []
# info 나누기
new_info = []
info_scores = []
for line in info:
tmp = list(map(str,line.split(' ')))
new_info.append(tmp[:-1])
info_scores.append(int(tmp[-1]))
# query 나누기
querys = []
q_scores = []
for q in query:
t = re.split(' and | ',q)
q_scores.append(int(t[-1]))
querys.append(t[:-1])
for i in range(len(querys)):
result = 0
for j in range(len(new_info)):
# 점수가 맞는 경우만 조건 따지기
if q_scores[i] <= info_scores[j]:
if querys[i] == new_info[j] and querys[i] == ['-' ,'-', '-', '-']:
result += 1
else:
flag = 0
for k in range(len(querys[i])):
if querys[i][k] == '-':
continue
elif querys[i][k] != new_info[j][k]:
flag = 1
break
else:
continue
if flag == 0:
result += 1
answer.append(result)
return answer
📌다른 사람들의 풀이
from itertools import combinations
from collections import defaultdict
from bisect import bisect_left
def solution(information, queries):
answer = []
dic = defaultdict(list)
for info in information:
info = info.split()
condition = info[:-1]
score = int(info[-1])
for i in range(5):
case = list(combinations([0,1,2,3], i))
for c in case:
tmp = condition.copy()
for idx in c:
tmp[idx] = "-"
key = ''.join(tmp)
dic[key].append(score)
for value in dic.values():
value.sort()
for query in queries:
query = query.replace("and ", "")
query = query.split()
target_key = ''.join(query[:-1])
target_score = int(query[-1])
count = 0
if target_key in dic:
target_list = dic[target_key]
idx = bisect_left(target_list, target_score)
count = len(target_list) - idx
answer.append(count)
return answer
📌리뷰
- 이분탐색 문제. 생각보다 변수 수가 적으면 dic 활용도 생각해보기
- Python 이분탐색은 bisect 활용
728x90