-
[Programmers] ์์ ๊ฒ์ with PythonPS 2021. 10. 14. 23:30728x90๋ฐ์ํ
๐ Programmers - [์์ ๊ฒ์]
๐ก ์กฐ๊ฑด ๋ฐ ํ์ด
์กฐ๊ฑด์ ๋ง์กฑํ๋ ์ฌ๋ ์ค
์ฝ๋ฉํ ์คํธ ์ ์๋ฅผ X์ ์ด์ ๋ฐ์ ์ฌ๋
์ ๋ชจ๋ ๋ช ๋ช ์ธ๊ฐ?
๋ฅผ ๊ตฌํ๋ ๋ฌธ์ '-'
ํ์๋ ํด๋น ์กฐ๊ฑด์ ๊ณ ๋ คํ์ง ์๊ฒ ๋ค๋ ์๋ฏธ."cpp and - and senior and pizza 500"
์
"cpp๋ก ์ฝ๋ฉํ ์คํธ๋ฅผ ๋ดค์ผ๋ฉฐ, ๊ฒฝ๋ ฅ์ senior ์ด๋ฉด์ ์์ธํธ๋๋ก pizza๋ฅผ ์ ํํ ์ง์์ ์ค ์ฝ๋ฉํ ์คํธ ์ ์๋ฅผ 500์ ์ด์ ๋ฐ์ ์ฌ๋์ ๋ชจ๋ ๋ช ๋ช ์ธ๊ฐ?"
๋ฅผ ์๋ฏธํ๋ค.
๋ธ๋ฃจํธํฌ์ค ์๊ณ ๋ฆฌ์ฆ ์ ํ์ ๋ฌธ์ ์ ํด๋นํ๋ค.
๐ฅ ์์ค ์ฝ๋
from itertools import combinations from bisect import bisect_left def solution(info, query): answer, data, sql = [], {}, [] n, m = len(info), len(query) for j in info: temp = j.split() condition = temp[:-1] score = int(temp[-1]) for i in range(5): comb = list(combinations(range(4), i)) for c in comb: test_case = condition.copy() for idx in c: test_case[idx] = '-' case = ''.join(test_case) if case not in data: data[case] = [score] else: data[case].append(score) for i in data.values(): i.sort() for i in range(m): sql = query[i].replace('and', '').split() test_query = ''.join(sql[:-1]) test_score = int(sql[-1]) if test_query in data: idx = bisect_left(data[test_query], test_score) answer.append(len(data[test_query]) - idx) else: answer.append(0) return answer
๐ ์์ ๋ฐ ์คํ๊ฒฐ๊ณผ
์์
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"]
์คํ๊ฒฐ๊ณผ
[1,1,1,1,2,4]
โจ๏ธ ๋ฌธ์ ํ์ด
info ๋ฐฐ์ด์ ์ํํ๋ฉฐ ์ป์ ๋ฐ์ดํฐ๋ฅผ ์๋ผ ๋ฐฐ์ด๋ก ๋ง๋ค๊ณ , ๊ทธ ๋ฐฐ์ด์ ๊ฐ๊ฐ ๋ฐ์ดํฐ์ ์ ์ ๋ถ๋ถ์ผ๋ก ๋๋์ด ์ค๋ค.
temp = j.split() condition = temp[:-1] score = int(temp[-1])
์ง์์์ ์ ๋ ฅํ 4๊ฐ์ ๊ฐ range(4)์ ๋ฐ์ดํฐ๋ฅผ combinations ์ ์ด์ฉํด
์ง์ ์ง์ด ๊ฐ 1๋ถํฐ 4๊ฐ๊น์ง์ ๊ฒฝ์ฐ์ ์์ ์ ์๋ฅผ dict ์๋ฃ๊ตฌ์กฐ์ ๋ฃ์ด์ค๋ค.for i in range(5): comb = list(combinations(range(4), i)) for c in comb: test_case = condition.copy() for idx in c: test_case[idx] = '-' case = ''.join(test_case) if case not in data: data[case] = [score] else: data[case].append(score)
์ด๋ถํ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ธ bisect ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํด ์ฌ์ฉ์ ํ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ dict ์๋ฃ๊ตฌ์กฐ์ value๋ฅผ ์ ๋ ฌํด์ค๋ค.
ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์์จ sql์ ์์ฐจ์ ์ผ๋ก ๋๋ฉด์ and ๋ฌธ์์ด์ ''๋ก ๋ฐ๊พธ์ด์ฃผ๊ณ split ํด์ค๋ค.
test_query ์ test_score ๋ก ๋๋์ด์ฃผ๊ณ , test_query์ ํด๋นํ๋ ์ธ์ ์ค(data์ key)
test_score ์ด์์ ์ ์๋ฅผ ์ป์(data์ value) ์ธ์์ ์๋ฅผ ๊ณ์ฐํ์ฌ answer์ ์ ๋ ฅํด์ค๋ค.
๐พ ๋๋์
- ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ data ์ ์ ๋ ฅํ์ฌ ์ฐพ๋ ์์ด๋์ด๋ฅผ ๊ตฌ์ํ๋ ๊ฒ์ด ํ์ด ๋ค์๋ค.
- sql์ ํด๋นํ๋ ์ง์์๋ฅผ ์ด๋ถํ์์ผ๋ก ์ฐพ์ ์์ด๋์ด์ dict ์๋ฃ๊ตฌ์กฐ๋ฅผ ์ฌ์ฉํ ์์ด๋์ด๋ฅผ ๋ ์ฌ๋ฆฌ๋
๊ตฌํํ๋๋ฐ์๋ ํฐ ๋ฌด๋ฆฌ๊ฐ ์์๋ ๊ฒ ๊ฐ๋ค. - ํฌ์คํ
๋ด์ฉ์ ๋ณด๋ ์์ ์์ด๋์ด๋ฅผ ์ป์ง ๋ชปํ ๋ถ๋ค์ด ๋ณด์๊ธฐ์ ๊ด์ฐฎ์๊น ๋ผ๋ ์๊ฐ์ด ๋ค๋ฉด์,
์ค๋ช ํ๋ ๋ฅ๋ ฅ์ด ์กฐ๊ธ ๋ถ์กฑํ๋ค๊ณ ๋๋๋ค.
๋ฐ์ํ'PS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Programmers] ํ ํธ์ง with Python (0) 2021.10.17 [Programmers] ๊ด๊ณ ์ฝ์ with Python (0) 2021.10.17 [Programmers] ๋ฉ๋ด ๋ฆฌ๋ด์ผ with Python (0) 2021.10.13 [๋ฐฑ์ค] 10775 ๊ณตํญ with Python (0) 2021.10.13 [๋ฐฑ์ค] 2143 ๋ ๋ฐฐ์ด์ ํฉ with Python (0) 2021.10.12