Chapter8. ์ด์ง ํ์
[๋ฌธ์ 30] ์์ ๊ฒ์ - Level2
์ง์์๊ฐ ์ง์์์ ์
๋ ฅํ 4๊ฐ์ง์ ์ ๋ณด์ ํ๋ํ ์ฝ๋ฉ ํ
์คํธ ์ ์๋ฅผ ํ๋์ ๋ฌธ์์ด๋ก ๊ตฌ์ฑํ ๊ฐ์ ๋ฐฐ์ด info, ๊ฐ๋ฐํ์ด ๊ถ๊ธํดํ๋ ๋ฌธ์ ์กฐ๊ฑด์ด ๋ฌธ์์ด ํํ๋ก ๋ด๊ธด ๋ฐฐ์ด query๊ฐ ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง ๋, ๊ฐ ๋ฌธ์ ์กฐ๊ฑด์ ํด๋นํ๋ ์ฌ๋๋ค์ ์ซ์๋ฅผ ์์๋๋ก ๋ฐฐ์ด์ ๋ด์ returnํ๋๋ก solution ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์.
[์ ํ์ฌํญ]
- info ๋ฐฐ์ด์ ํฌ๊ธฐ๋ 1 ์ด์ 50,000 ์ดํ
- info ๋ฐฐ์ด ๊ฐ ์์์ ๊ฐ์ ์ง์์๊ฐ ์ง์์์ ์
๋ ฅํ 4๊ฐ์ง ๊ฐ๊ณผ ์ฝ๋ฉ ํ
์คํธ ์ ์๋ฅผ ํฉ์น "๊ฐ๋ฐ์ธ์ด ์ง๊ตฐ ๊ฒฝ๋ ฅ ์์ธํธ๋ ์ ์"ํ์
- ๊ฐ๋ฐ์ธ์ด๋ cpp, java, python ์ค ํ๋
- ์ง๊ตฐ์ backend, frontend ์ค ํ๋
- ๊ฒฝ๋ ฅ์ junior, senior ์ค ํ๋
- ์์ธํธ๋๋ chicken, pizza ์ค ํ๋
- ์ ์๋ ์ฝ๋ฉ ํ
์คํธ ์ ์๋ฅผ ์๋ฏธํ๋ฉฐ, 1 ์ด์ 100,000 ์ดํ์ธ ์์ฐ์
- ๊ฐ ๋จ์ด๋ ๊ณต๋ฐฑ๋ฌธ์(์คํ์ด์ค ๋ฐ) ํ๋๋ก ๊ตฌ๋ถ
- query ๋ฐฐ์ด์ ํฌ๊ธฐ๋ 1 ์ด์ 100,000 ์ดํ
- query์ ๊ฐ ๋ฌธ์์ด์ "[์กฐ๊ฑด]X"ํ์
- [์กฐ๊ฑด]์ "๊ฐ๋ฐ์ธ์ด and ์ง๊ตฐ and ๊ฒฝ๋ ฅ and ์์ธํธ๋"ํ์์ ๋ฌธ์์ด์
๋๋ค.
- ์ธ์ด๋ cpp, java, python, -์ค ํ๋
- ์ง๊ตฐ์ backend, frontend, -์ค ํ๋
- ๊ฒฝ๋ ฅ์ junior, senior, -์ค ํ๋
- ์์ธํธ๋๋ chicken, pizza, -์ค ํ๋
- '-'ํ์๋ ํด๋น ์กฐ๊ฑด์ ๊ณ ๋ คํ์ง ์๊ฒ ๋ค๋ ์๋ฏธ
- X๋ ์ฝ๋ฉ ํ
์คํธ ์ ์๋ฅผ ์๋ฏธํ๋ฉฐ ์กฐ๊ฑด์ ๋ง์กฑํ๋ ์ฌ๋ ์ค X์ ์ด์ ๋ฐ์ ์ฌ๋์ ๋ชจ๋ ๋ช ๋ช
์ธ์ง๋ฅผ ์๋ฏธ
- ๊ฐ ๋จ์ด๋ ๊ณต๋ฐฑ๋ฌธ์(์คํ์ด์ค ๋ฐ) ํ๋๋ก ๊ตฌ๋ถ
- ex) "cpp and - and senior and pizza 500"์ 'cpp๋ก ์ฝ๋ฉ ํ
์คํธ๋ฅผ ๋ดค์ผ๋ฉฐ, ๊ฒฝ๋ ฅ์ senior์ด๋ฉด์ ์์ธํธ๋๋ก pizza๋ฅผ ์ ํํ ์ง์์ ์ค ์ฝ๋ฉ ํ
์คํธ ์ ์๋ฅผ 500์ ์ด์ ๋ฐ์ ์ฌ๋์ ๋ชจ๋ ๋ช ๋ช
์ธ๊ฐ?'๋ฅผ ์๋ฏธ
[๋ฌธ์ ํ์ด]
- ์ง์์๋ ์ฃผ์ด์ง 4๊ฐ์ ์ ํ์ง๋ฅผ ๋ชจ๋ ๊ณจ๋ผ์ผ ํ๋ฉฐ, ์ฑ์ ๊ณผ ํจ๊ผ ๋ฐ์ดํฐ๊ฐ ์ ์ฅ๋๋ค.
- ์ฝ๋๋ก ๊ตฌํํ ์ฌํญ์ ์ฃผ์ด์ง ๋ฐ์ดํฐ์ ์กฐ๊ฑด์ ๊ธฐ๋ฐ์ผ๋ก ๊ฒฐ๊ณผ๋ฅผ ์ถ์ถํ์ฌ ๋ฐํํ๋ ๊ฒ.
[์ฝ๋์์ฑ]
- ์ง์์์ ์ ํ ์ฌํญ๊ณผ ์ ์๊ฐ ๋ด๊ธด ๋ฌธ์์ด ๋ฐฐ์ด, ๋ฐ์ดํฐ ์กฐํ ๋ช
๋ น์ด ๋ด๊ธด ๋ฌธ์์ด ๋ฐฐ์ด์ ์ ์ฒ๋ฆฌ
def solution(info, query):
data = [i.split() for i in info]
queries = []
for q in query:
q = q.split()
for _ in range(3): q.remove('and')
queries.append(q)
- ๋ฌธ์๋ณ๋ก ์ง์์์ ๋ฐ์ดํฐ๋ฅผ ๊ฒ์ฌํ์ฌ ์กฐ๊ฑด์ ๋ง๋ ์ ๋ต์ ์ฐพ๋๋ค.
answer = [0]
for i in range(len(queries)):
q = queries[i]
for info in data:
for j in range(5):
if q[j] == '-': continue
elif j == 4 and int(info[j]) >= int(q[j]): answer[i] += 1
elif info[j] != q[j]: break
return answer
[์ ์ฒด์ฝ๋]
def solution(info, query):
data = [i.split() for i in info]
queries = []
for q in query:
q = q.split()
for _ in range(3): q.remove('and')
queries.append(q)
answer = [0] * len(query)
for i in range(len(queries)):
q = queries[i]
for info in data:
for j in range(5):
if q[j] == '-': continue
elif j == 4 and int(info[j]) >= int(q[j]): answer[i] += 1
elif info[j] != q[j]: break
return answer
[๋ค๋ฅธ ํ์ด]
- ๋ฏธ๋ฆฌ ๋์
๋๋ฆฌ ๋ฐ์ดํฐ๋ฅผ ๋ง๋ค์ด๋์ ๋ณ์ ์ ์ธ
from collections import defaultdict
def solution(info, query):
answer = []
people = defaultdict(list)
- ์ฃผ์ด์ง ๋ชจ๋ ์ง์์์ ๋ฐ์ดํฐ๋ฅผ for ๋ฌธ์ผ๋ก ์ํํ๋ฉด์ ๊ฐ๋ฅํ ๊ฒฝ์ฐ์ ์๋ฅผ ๋์
๋๋ฆฌ์ ๊ธฐ๋ก
from itertools import combinations
for i in info:
person = i.split()
score = int(person.pop())
people[''.join(person)].append(score)
for j in range(4):
case = list(combinations(person, j))
for c in case:
people[''.join(c)].append(score)
- ๊ธฐ๋กํ ๋์
๋๋ฆฌ์ ์ฑ์ ๋ฐ์ดํฐ๋ฅผ ๋ชจ๋ ์ ๋ ฌ
for i in people: people[i].sort()
- ๋ฌธ์ ์กฐ๊ฑด์ ๋ฐ๋ผ ๊ฒ์ํ๊ณ , ๋์จ ๊ฒฐ๊ณผ์ ์ฑ์ ๋ฐฐ์ด์ ์ด์ง ํ์ํ์ฌ ๋ช ๋ช
์ธ์ง ํ์ธ
from bisect import bisect_left as left_bound
for i in query:
key = i.split()
score = int(key.pop())
key = ''.join(key)
key = key.replce('and', '').replace(' ', '').replace('-', '')
answer.append(len(people[key]) - left_bound(people[key], score))
return answer
[์ ์ฒด์ฝ๋]
from itertools import combinations
from collections import defaultdict
from bisect import bisect_left as left_bound
def solution(info, query):
answer = []
people = defaultdict(list)
for i in info:
person = i.split()
score = int(person.pop())
people[''.join(person)].append(score)
for j in range(4):
case = list(combinations(person, j))
for c in case:
people[''.join(c)].append(score)
for i in people: people[i].sort()
for i in query:
key = i.split()
score = int(key.pop())
key = ''.join(key)
key = key.replce('and', '').replace(' ', '').replace('-', '')
answer.append(len(people[key]) - left_bound(people[key], score))
return answer