๐Ÿ’ป์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๋ฌธ์ œํ’€์ด8

์ง€๋ฏผ์„œยท2023๋…„ 3์›” 13์ผ
0

coding test

๋ชฉ๋ก ๋ณด๊ธฐ
8/30

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
profile
๐Ÿ’ป + ๐ŸŽฅ

0๊ฐœ์˜ ๋Œ“๊ธ€