[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))



Categories:

Updated:

Leave a comment