[Programmers] ์์ ๊ฒ์
Updated:
์์ ๊ฒ์
์์ย ๊ฒ์ ์ ํด๋ฆญํ๋ฉด ๋ฐ๋ก ์ด๋ํ๋ค.
์ ํ์ฑ์ ํต๊ณผํ์ง๋ง ํจ์จ์ฑ์ ํต๊ณผํ์ง ๋ชปํ๋ ๋ฌธ์ ์ด๋ค.
ํจ์จ์ฑ์ ํต๊ณผํ๋ ค๋ฉด ์ฟผ๋ฆฌ๋ง๋ค ์ง์์๋ฅผ ๊ฒ์ํ๋ฉด ์ ๋๊ณ ์ง์์ ์ ๋ณด๋ฅผ ๋ฏธ๋ฆฌ ์ ์ฅํด๋์ด์ผ ํ๋ค.
๋ฐ๋ผ์ ๊ฐ๋ฅํ ๋ชจ๋ ์ฟผ๋ฆฌ๋ฌธ์ ํค๊ฐ์ผ๋ก ํ๋ ์ฌ์ ์ ๋ง๋ค๊ณ ์ง์์ ์ ์๋ฅผ ๊ฒ์์ด ๊ฐ๋ฅํ ๋ชจ๋ ์ฟผ๋ฆฌ์ ๋ฃ์ด์ค๋ค.
์ดํ์ ํด๋น ์ฟผ๋ฆฌ์ ํน์ ์ ์ ์ด์์ ์ฌ๋ ์๋ฅผ ๊ตฌํ๋ฉด ๋๋ค.
์ฌ๋ ์๋ฅผ ๊ตฌํ๋ ๊ฒฝ์ฐ์๋ ์ผ์ผ์ด ์ธ๋ฉด ์๊ฐ ์ด๊ณผ๊ฐ ๋ฐ์ํ๊ธฐ ๋๋ฌธ์ ์ด์ง ํ์์ผ๋ก ์ฐพ์์ผ ํ๋ค.
ํด๋น ์ฝ๋๋ ํ๋ก๊ทธ๋๋จธ์ค ์ ๋ต์์ ์ฝ๋๋ฅผ ์ฐธ์กฐํ๋ค.
import bisect
def solution(info, query):
answer = []
query_dict = dict()
# 1. ๊ฐ๋ฅํ ๋ชจ๋ ์ฟผ๋ฆฌ๋ฌธ ์์ฑ
for a in ['java', 'python', 'cpp', '-']:
for b in ['backend', 'frontend', '-']:
for c in ['junior', 'senior', '-']:
for d in ['pizza', 'chicken', '-']:
query_dict.setdefault((a, b, c, d), [])
for data in info:
split_data = data.split()
user_info = split_data[:-1]
user_score = int(split_data[-1])
# 2. ์กฐ๊ฑด์ ์ถฉ์กฑํ๋ ์ฟผ๋ฆฌ๋ฌธ์ ์ ์ ์ถ๊ฐ
for a in [user_info[0], '-']:
for b in [user_info[1], '-']:
for c in [user_info[2], '-']:
for d in [user_info[3], '-']:
query_dict[a, b, c, d].append(user_score)
for value in query_dict.values():
value.sort()
# 3. ์ฟผ๋ฆฌ๋ฌธ์ ํตํด ํน์ ์ ์ ์ด์์ ์ฌ๋ ์ฐพ๊ธฐ
for data in query:
split_data = data.split()
split_query = (split_data[0], split_data[2], split_data[4], split_data[6])
query_score = int(split_data[7])
size = len(query_dict[split_query])
answer.append(size - bisect.bisect_left(query_dict[split_query], query_score))
return answer
if __name__ == "__main__":
info = ["java backend junior pizza 150",
"python frontend senior chicken 210",
"python frontend senior chicken 150",
"cpp backend senior pizza 260",
"java backend junior chicken 80",
"python backend senior chicken 50"]
query = ["java and backend and junior and pizza 100",
"python and frontend and senior and chicken 200",
"cpp and - and senior and pizza 250",
"- and backend and senior and - 150",
"- and - and - and chicken 100",
"- and - and - and - 150"]
print(solution(info, query))
Leave a comment