[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ˆœ์œ„ ๊ฒ€์ƒ‰ / ํŒŒ์ด์ฌ / Python / 2021 KAKAO BLIND RECRUITMENT / ์นด์นด์˜ค ์ฝ”ํ…Œ ๊ธฐ์ถœ
Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ˆœ์œ„ ๊ฒ€์ƒ‰ / ํŒŒ์ด์ฌ / Python / 2021 KAKAO BLIND RECRUITMENT / ์นด์นด์˜ค ์ฝ”ํ…Œ ๊ธฐ์ถœ

728x90
๋ฐ˜์‘ํ˜•

 

๐Ÿ’กsolutions )

๐Ÿ’ฌ ํ•ด๋‹น ๋ฌธ์ œ๋Š” ์ •ํ™•์„ฑ๊ณผ ํšจ์œจ์„ฑ ๋ชจ๋‘ ๋งŒ์กฑํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ๋‹ค.

 

๐Ÿ’ฌ ์ฒ˜์Œ ์ž‘์„ฑํ–ˆ๋˜ ์ฝ”๋“œ๋Š” ์ •ํ™•์„ฑ๋งŒ ํ†ต๊ณผํ–ˆ๊ณ , ํšจ์œจ์„ฑ์„ ๋งŒ์กฑํ•˜์ง€ ๋ชปํ–ˆ๋‹ค. for๋ฌธ์„ ํ†ตํ•ด ์ฃผ์–ด์ง„ query์ •๋ณด์™€  info๋ฅผ ๋ชจ๋‘ ๋น„๊ตํ•˜์—ฌ ๋งŒ์กฑํ•˜๋Š” ์ง€์›์ž๊ฐ€ ๋ช‡ ๋ช…์ธ์ง€ ๊ตฌํ•˜๋ฉด ์ •ํ™•์„ฑ์€ ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋‹ค.

 

๐Ÿ’ฌ ๋ฌธ์ œ๋Š” ํšจ์œจ์„ฑ. ํ˜ผ์ž์„œ ํ•ด๊ฒฐํ•˜์ง€ ๋ชปํ•ด์„œ '์นด์นด์˜ค ํ…Œํฌ ๋ธ”๋กœ๊ทธ'์™€ ๋‹ค๋ฅธ ๋ถ„์˜ ํ’€์ด๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉฐ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ•ด๋ดค๋‹ค.

 

๐Ÿ’ฌ ๋จผ์ €, ํ•ด์‹œ๋งต์„ ํ™œ์šฉํ•ด ์ง€์›์ž๋“ค์˜ ์ •๋ณด๋ฅผ ๋ฏธ๋ฆฌ ์ €์žฅํ•ด๋‘๋Š” ๋กœ์ง์ด ํ•„์š”ํ–ˆ๋‹ค. -> ์ฃผ์–ด์ง„ info ์ •๋ณด๋ฅผ key๊ฐ’์œผ๋กœ ์ ์ˆ˜๋ฅผ value๋กœ ํ•˜์—ฌ ๋”•์…”๋„ˆ๋ฆฌ์— ์ €์žฅ -> ํ•˜๋‚˜์˜ info์— ์ด 16๊ฐ€์ง€์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ๋งŒ๋“ค์–ด์ง„๋‹ค. -> info์—์„œ '-'๋ฅผ ๊ณ ๋ คํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ๋”ฐ๋กœ '-'๋ฌธ์ž๋ฅผ ๋„ฃ์ง€ ์•Š๊ณ  ์ฃผ์–ด์ง„ info ์กฐ๊ฑด๋“ค๋งŒ์„ ๋ชจ์•„ key๊ฐ’์„ ๋งŒ๋“ค์—ˆ๋‹ค.

 

e.g) python - junior pizza์ธ ๊ฒฝ์šฐ 'python-juniorpizza'๊ฐ€ ์•„๋‹Œ 'pythonjuniorpizza'๋กœ ๋งŒ๋“œ๋Š” ์‹

ํ•„์ž์˜ ๊ฒฝ์šฐ๋Š” ์ถ”ํ›„ query๋ฌธ์—์„œ '-'๋ฅผ ์ œ๊ฑฐํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— query ์กฐ๊ฑด๊ณผ info ์ •๋ณด๋ฅผ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋น„๊ตํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

๐Ÿ’ฌ query์กฐ๊ฑด๊ณผ info ์ •๋ณด๊ฐ€ ์ผ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ query_score๋ณด๋‹ค ๋†’์€ ์ ์ˆ˜์˜ ์ง€์›์ž๊ฐ€ ๋ช‡ ๋ช…์ธ์ง€ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด binary search๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. -> ์ด๋ฅผ ์œ„ํ•ด info_dict์˜ value๊ฐ’๋“ค์€ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค.

 

๐Ÿ’ฌlower bound ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜์—ฌ query_score๋ณด๋‹ค ๋†’์€ ์ ์ˆ˜์˜ ์ง€์›์ž๊ฐ€ ๋ช‡ ๋ช…์ธ์ง€ ๊ตฌํ•œ๋‹ค.

lower bound ?  binary search์—์„œ ํŒŒ์ƒ๋œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ '์›ํ•˜๋Š” ๊ฐ’ ์ด์ƒ์ด ์ฒ˜์Œ ๋‚˜์˜ค๋Š” ์œ„์น˜๋ฅผ ์ฐพ๋Š” ๊ณผ์ •'์ด๋‹ค.

 

๐Ÿ‘จ‍๐Ÿ’ปcode )

# ์ •ํ™•์„ฑ, ํšจ์œจ์„ฑ ๋ชจ๋‘ ํ†ต๊ณผํ•œ ์ฝ”๋“œ๐Ÿ‘‡

from itertools import combinations as combi
from collections import defaultdict


# ์ ์ˆ˜๋ฅผ ์ œ์™ธํ•œ info ์ •๋ณด๋ฅผ key๊ฐ’์œผ๋กœ, ์ ์ˆ˜๋ฅผ value๊ฐ’์œผ๋กœ ํ•ด์‹œ๋งต ์ƒ์„ฑ
def solution(infos, queries):
    answer = []
    info_dict = defaultdict(list)
    for info in infos:
        info = info.split()
        info_key = info[:-1]
        info_val = int(info[-1])
        for i in range(5):
            for c in combi(info_key, i):  # ํ•˜๋‚˜์˜ info์—์„œ ๊ฒฝ์šฐ์˜ ์ˆ˜ 16๊ฐœ ๋งŒ๋“ค๊ธฐ
                tmp_key = ''.join(c)
                info_dict[tmp_key].append(info_val)  # ๊ฐ€๋Šฅํ•œ info ์กฐํ•ฉ์„ key, ์ ์ˆ˜๋ฅผ value๋กœ ๋”•์…”๋„ˆ๋ฆฌ ์ €์žฅ
    for key in info_dict.keys():
        info_dict[key].sort()  # value๊ฐ’ ์ ์ˆ˜๋“ค์€ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ฆฌ

    for query in queries:
        query = query.split(' ')
        query_score = int(query[-1])
        query = query[:-1]

        for i in range(3):  # and ์ œ๊ฑฐ
            query.remove('and')
        while '-' in query:  # '-'์ œ๊ฑฐ
            query.remove('-')
        tmp_q = ''.join(query)

        # lower bound ์‚ฌ์šฉํ•ด query_score ๋ณด๋‹ค ํฐ ์ ์ˆ˜๋“ค์˜ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ
        if tmp_q in info_dict:
            scores = info_dict[tmp_q]
            if len(scores) > 0:
                start, end = 0, len(scores)
                while end > start:
                    mid = (start + end) // 2
                    if scores[mid] >= query_score:
                        end = mid
                    else:
                        start = mid + 1
                answer.append(len(scores) - start)
        else:
            answer.append(0)
    return answer

# ์ •ํ™•์„ฑ๋งŒ ํ†ต๊ณผํ•œ ์ฝ”๋“œ ๐Ÿ‘‡

def solution(info, query):
    infos = [i.split() for i in info]
    queries = []  # ๊ฐ ์›์†Œ 8๊ฐœ์”ฉ
    for q in query:
        q = q.split()
        for i in range(3):
            q.remove('and')
        queries.append(q)

    res = [0] * len(query)
    for i in range(len(queries)):
        for info_item in infos:
            for k in range(5):
                if queries[i][k] == '-':
                    continue
                elif k == 4:
                    if int(info_item[k]) >= int(queries[i][k]):
                        res[i] += 1
                elif info_item[k] != queries[i][k]:
                    break

    return res

๐Ÿ“Œdescription )

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ˆœ์œ„ ๊ฒ€์ƒ‰

["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"] ["java and backend and junior and pizza 100","pyt

programmers.co.kr


 

์นด์นด์˜ค๋Š” ํ•˜๋ฐ˜๊ธฐ ๊ฒฝ๋ ฅ ๊ฐœ๋ฐœ์ž ๊ณต๊ฐœ์ฑ„์šฉ์„ ์ง„ํ–‰ ์ค‘์— ์žˆ์œผ๋ฉฐ ํ˜„์žฌ ์ง€์›์„œ ์ ‘์ˆ˜์™€ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ๊ฐ€ ์ข…๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ„์šฉ์—์„œ ์ง€์›์ž๋Š” ์ง€์›์„œ ์ž‘์„ฑ ์‹œ ์•„๋ž˜์™€ ๊ฐ™์ด 4๊ฐ€์ง€ ํ•ญ๋ชฉ์„ ๋ฐ˜๋“œ์‹œ ์„ ํƒํ•˜๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

  • ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์ฐธ์—ฌ ๊ฐœ๋ฐœ์–ธ์–ด ํ•ญ๋ชฉ์— cpp, java, python ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ์ง€์› ์ง๊ตฐ ํ•ญ๋ชฉ์— backend์™€ frontend ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ์ง€์› ๊ฒฝ๋ ฅ๊ตฌ๋ถ„ ํ•ญ๋ชฉ์— junior์™€ senior ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ์„ ํ˜ธํ•˜๋Š” ์†Œ์šธํ‘ธ๋“œ๋กœ chicken๊ณผ pizza ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ธ์žฌ์˜์ž…ํŒ€์— ๊ทผ๋ฌดํ•˜๊ณ  ์žˆ๋Š” ๋‹ˆ๋‹ˆ์ฆˆ๋Š” ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ์ฑ„์šฉ์— ์ฐธ์—ฌํ•œ ๊ฐœ๋ฐœํŒ€๋“ค์— ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด ์ง€์›์ž๋“ค์˜ ์ง€์› ์กฐ๊ฑด์„ ์„ ํƒํ•˜๋ฉด ํ•ด๋‹น ์กฐ๊ฑด์— ๋งž๋Š” ์ง€์›์ž๊ฐ€ ๋ช‡ ๋ช…์ธ ์ง€ ์‰ฝ๊ฒŒ ์•Œ ์ˆ˜ ์žˆ๋Š” ๋„๊ตฌ๋ฅผ ๋งŒ๋“ค๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๊ฐœ๋ฐœํŒ€์—์„œ ๊ถ๊ธˆํ•ดํ•˜๋Š” ๋ฌธ์˜์‚ฌํ•ญ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•ํƒœ๊ฐ€ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฝ”๋”ฉํ…Œ์ŠคํŠธ์— java๋กœ ์ฐธ์—ฌํ–ˆ์œผ๋ฉฐ, backend ์ง๊ตฐ์„ ์„ ํƒํ–ˆ๊ณ , junior ๊ฒฝ๋ ฅ์ด๋ฉด์„œ, ์†Œ์šธํ‘ธ๋“œ๋กœ pizza๋ฅผ ์„ ํƒํ•œ ์‚ฌ๋žŒ ์ค‘ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์ ์ˆ˜๋ฅผ 50์  ์ด์ƒ ๋ฐ›์€ ์ง€์›์ž๋Š” ๋ช‡ ๋ช…์ธ๊ฐ€? ๋ฌผ๋ก  ์ด ์™ธ์—๋„ ๊ฐ ๊ฐœ๋ฐœํŒ€์˜ ์ƒํ™ฉ์— ๋”ฐ๋ผ ์•„๋ž˜์™€ ๊ฐ™์ด ๋‹ค์–‘ํ•œ ํ˜•ํƒœ์˜ ๋ฌธ์˜๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ์ฝ”๋”ฉํ…Œ์ŠคํŠธ์— python์œผ๋กœ ์ฐธ์—ฌํ–ˆ์œผ๋ฉฐ, frontend ์ง๊ตฐ์„ ์„ ํƒํ–ˆ๊ณ , senior ๊ฒฝ๋ ฅ์ด๋ฉด์„œ, ์†Œ์šธํ‘ธ๋“œ๋กœ chicken์„ ์„ ํƒํ•œ ์‚ฌ๋žŒ ์ค‘ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์ ์ˆ˜๋ฅผ 100์  ์ด์ƒ ๋ฐ›์€ ์‚ฌ๋žŒ์€ ๋ชจ๋‘ ๋ช‡ ๋ช…์ธ๊ฐ€?
  • ์ฝ”๋”ฉํ…Œ์ŠคํŠธ์— cpp๋กœ ์ฐธ์—ฌํ–ˆ์œผ๋ฉฐ, senior ๊ฒฝ๋ ฅ์ด๋ฉด์„œ, ์†Œ์šธํ‘ธ๋“œ๋กœ pizza๋ฅผ ์„ ํƒํ•œ ์‚ฌ๋žŒ ์ค‘ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์ ์ˆ˜๋ฅผ 100์  ์ด์ƒ ๋ฐ›์€ ์‚ฌ๋žŒ์€ ๋ชจ๋‘ ๋ช‡ ๋ช…์ธ๊ฐ€?
  • backend ์ง๊ตฐ์„ ์„ ํƒํ–ˆ๊ณ , senior ๊ฒฝ๋ ฅ์ด๋ฉด์„œ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์ ์ˆ˜๋ฅผ 200์  ์ด์ƒ ๋ฐ›์€ ์‚ฌ๋žŒ์€ ๋ชจ๋‘ ๋ช‡ ๋ช…์ธ๊ฐ€?
  • ์†Œ์šธํ‘ธ๋“œ๋กœ chicken์„ ์„ ํƒํ•œ ์‚ฌ๋žŒ ์ค‘ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์ ์ˆ˜๋ฅผ 250์  ์ด์ƒ ๋ฐ›์€ ์‚ฌ๋žŒ์€ ๋ชจ๋‘ ๋ช‡ ๋ช…์ธ๊ฐ€?
  • ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์ ์ˆ˜๋ฅผ 150์  ์ด์ƒ ๋ฐ›์€ ์‚ฌ๋žŒ์€ ๋ชจ๋‘ ๋ช‡ ๋ช…์ธ๊ฐ€?

[๋ฌธ์ œ]

์ง€์›์ž๊ฐ€ ์ง€์›์„œ์— ์ž…๋ ฅํ•œ 4๊ฐ€์ง€์˜ ์ •๋ณด์™€ ํš๋“ํ•œ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์ ์ˆ˜๋ฅผ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ๊ตฌ์„ฑํ•œ ๊ฐ’์˜ ๋ฐฐ์—ด info, ๊ฐœ๋ฐœํŒ€์ด ๊ถ๊ธˆํ•ดํ•˜๋Š” ๋ฌธ์˜์กฐ๊ฑด์ด ๋ฌธ์ž์—ด ํ˜•ํƒœ๋กœ ๋‹ด๊ธด ๋ฐฐ์—ด query๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๊ฐ ๋ฌธ์˜์กฐ๊ฑด์— ํ•ด๋‹นํ•˜๋Š” ์‚ฌ๋žŒ๋“ค์˜ ์ˆซ์ž๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด ์ฃผ์„ธ์š”.


 

๋ฐ˜์‘ํ˜•