PS

[Programmers] ์ˆœ์œ„ ๊ฒ€์ƒ‰ with Python

ํ˜•์ค€_It's 2021. 10. 14. 23:30
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ“Œ Programmers - [์ˆœ์œ„ ๊ฒ€์ƒ‰]

๐Ÿ’ก ์กฐ๊ฑด ๋ฐ ํ’€์ด

  1. ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์‚ฌ๋žŒ ์ค‘ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์ ์ˆ˜๋ฅผ X์  ์ด์ƒ ๋ฐ›์€ ์‚ฌ๋žŒ์€ ๋ชจ๋‘ ๋ช‡ ๋ช…์ธ๊ฐ€?
    ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ

  2. '-' ํ‘œ์‹œ๋Š” ํ•ด๋‹น ์กฐ๊ฑด์„ ๊ณ ๋ คํ•˜์ง€ ์•Š๊ฒ ๋‹ค๋Š” ์˜๋ฏธ.

  3. "cpp and - and senior and pizza 500"

    ์€

    "cpp๋กœ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ๋ฅผ ๋ดค์œผ๋ฉฐ, ๊ฒฝ๋ ฅ์€ senior ์ด๋ฉด์„œ ์†Œ์šธํ‘ธ๋“œ๋กœ pizza๋ฅผ ์„ ํƒํ•œ ์ง€์›์ž ์ค‘ 
    ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์ ์ˆ˜๋ฅผ 500์  ์ด์ƒ ๋ฐ›์€ ์‚ฌ๋žŒ์€ ๋ชจ๋‘ ๋ช‡ ๋ช…์ธ๊ฐ€?"

    ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

  4. ๋ธŒ๋ฃจํŠธํฌ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์œ ํ˜•์˜ ๋ฌธ์ œ์— ํ•ด๋‹นํ•œ๋‹ค.

๐Ÿ–ฅ ์†Œ์Šค ์ฝ”๋“œ

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]

โŒจ๏ธ ๋ฌธ์ œ ํ’€์ด

  1. info ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ์–ป์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ž˜๋ผ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค๊ณ , ๊ทธ ๋ฐฐ์—ด์„ ๊ฐ๊ฐ ๋ฐ์ดํ„ฐ์™€ ์ ์ˆ˜ ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋ˆ„์–ด ์ค€๋‹ค.

    temp = j.split()
    condition = temp[:-1]
    score = int(temp[-1])
  2. ์ง€์›์„œ์— ์ž…๋ ฅํ•œ 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)
  3. ์ด๋ถ„ํƒ์ƒ‰ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ bisect ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด ์‚ฌ์šฉ์„ ํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— dict ์ž๋ฃŒ๊ตฌ์กฐ์˜ value๋ฅผ ์ •๋ ฌํ•ด์ค€๋‹ค.

  4. ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์•„์˜จ sql์„ ์ˆœ์ฐจ์ ์œผ๋กœ ๋Œ๋ฉด์„œ and ๋ฌธ์ž์—ด์„ ''๋กœ ๋ฐ”๊พธ์–ด์ฃผ๊ณ  split ํ•ด์ค€๋‹ค.
    test_query ์™€ test_score ๋กœ ๋‚˜๋ˆ„์–ด์ฃผ๊ณ , test_query์— ํ•ด๋‹นํ•˜๋Š” ์ธ์› ์ค‘(data์˜ key)
    test_score ์ด์ƒ์˜ ์ ์ˆ˜๋ฅผ ์–ป์€(data์˜ value) ์ธ์›์˜ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ answer์— ์ž…๋ ฅํ•ด์ค€๋‹ค.

๐Ÿ’พ ๋А๋‚€์ 

  • ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ data ์— ์ž…๋ ฅํ•˜์—ฌ ์ฐพ๋Š” ์•„์ด๋””์–ด๋ฅผ ๊ตฌ์ƒํ•˜๋Š” ๊ฒƒ์ด ํž˜์ด ๋“ค์—ˆ๋‹ค.
  • sql์— ํ•ด๋‹นํ•˜๋Š” ์ง€์›์ž๋ฅผ ์ด๋ถ„ํƒ์ƒ‰์œผ๋กœ ์ฐพ์„ ์•„์ด๋””์–ด์™€ dict ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•  ์•„์ด๋””์–ด๋ฅผ ๋– ์˜ฌ๋ฆฌ๋‹ˆ
    ๊ตฌํ˜„ํ•˜๋Š”๋ฐ์—๋Š” ํฐ ๋ฌด๋ฆฌ๊ฐ€ ์—†์—ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.
  • ํฌ์ŠคํŒ… ๋‚ด์šฉ์„ ๋ณด๋‹ˆ ์•„์˜ˆ ์•„์ด๋””์–ด๋ฅผ ์–ป์ง€ ๋ชปํ•œ ๋ถ„๋“ค์ด ๋ณด์‹œ๊ธฐ์— ๊ดœ์ฐฎ์„๊นŒ ๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ค๋ฉด์„œ,
    ์„ค๋ช…ํ•˜๋Š” ๋Šฅ๋ ฅ์ด ์กฐ๊ธˆ ๋ถ€์กฑํ•˜๋‹ค๊ณ  ๋А๋‚€๋‹ค.
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€์ˆ˜0