반응형
Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 2020카카오
- 투잡
- 코테
- 백준
- 2021카카오
- 2018카카오
- 구글애드몹
- pccp
- 코딩테스크
- 2018캐시
- 코딩테스트
- 부수입
- 콜랩오류
- 브루드포스
- ai부수입
- AI논문
- 코딩오류
- 바이브코딩
- 에라스테네스의 체
- 프로그래머스
- 그리디
- 파이썬
- 앱제작
- DP
- 논문추천
- 백트레킹
- 밸만포드알고리즘
- 밸만포드
- Python
- 투포인터
Archives
- Today
- Total
kiteday 님의 블로그
[프로그래머스-파이썬] 순위검색 본문
반응형
SMALL
https://school.programmers.co.kr/learn/courses/30/lessons/72412
def solution(info, query):
answer = []
q = len(query)
people = []
terms = []
for i in info:
person = list(i.split())
people.append(person)
for j in query:
term = list(j.split())
term = [t for t in term if t != 'and']
terms.append(term)
for t in terms:
candidate = [p for p in people if int(p[-1])>= int(t[-1])]
for k in range(4):
if t[k] == '-':
continue
candidate = [c for c in candidate if c[k]==t[k]]
answer.append(len(candidate))
return answer
이렇게 풀면 문제는 풀리지만 효율성에서 탈락이다. 왜냐면 candidate를 비교하는데 너무 많은 자원이 들기 때문이다. 해당 코드는 O(len(query)*N)의 시간복잡도를 가지기 때문에 query가 많아질수록 결과적으로 O(N^2)의 시간복잡도를 가져서 효율성에서 0점 나온다..
from itertools import combinations
from bisect import bisect_left
def solution(info, query):
answer = []
data = {}
for applicant in info:
applicant = (applicant.split())
score = int(applicant[-1])
applicant = applicant[:-1]
for i in range(5):
for c in combinations(range(4), i):
key_list = list(applicant)
for k in c:
key_list[k] = '-'
key = tuple(key_list)
if key not in data:
data[key] = [score]
else:
data[key].append(score)
for key in data:
data[key].sort()
for q in query:
q = q.replace("and ", "").split()
q_score = int(q[-1])
q = tuple(q[:-1])
if q in data:
scores = data[q]
target = bisect_left(scores, q_score)
answer.append(len(scores)-target)
else:
answer.append(0)
return answer
효율성검사를 위한 새코드이다. 질문하기가 70개가 넘길래 만만하지 않다고 생각은 했는데 아이디어를 보고서도 쉽진 않았다.

이 코드의 방식은 1. info로 여러 키가 될 수 있는 조합을 미리 만들 것 2. 그 조합이 실제 query에 있는지 확인할 것.
코드상 특이점은 bisect_left로 개수를 체크했단거? 이 함수는 O(logN)의 시간복잡도를 가지고 있다고 한다. 그냥 for문을 쓰면 또 O(n)만큼 쓰기 때문에 효율성 검사에서 탈락이다.
아 문제를 푸는 것보다 잘푸는 건 훨씬 어렵다.
LIST
'코딩테스트' 카테고리의 다른 글
| [프로그래머스-파이썬] 광고 삽입 (0) | 2025.12.02 |
|---|---|
| [프로그래머스-파이썬] 보석쇼핑 (0) | 2025.12.02 |
| [프로그래머스-파이썬] 자물쇠와 열쇠 (0) | 2025.12.01 |
| [프로그래머스-파이썬] 합승 택시 요금 (0) | 2025.12.01 |
| [프로그래머스-파이썬] 혼자서 하는 틱택토 (1) | 2025.11.30 |