ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Programmers] ๋ฉ”๋‰ด ๋ฆฌ๋‰ด์–ผ with Python
    PS 2021. 10. 13. 23:58
    728x90
    ๋ฐ˜์‘ํ˜•

    ๐Ÿ“Œ Programmers - [๋ฉ”๋‰ด ๋ฆฌ๋‰ด์–ผ]

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

    1. orders ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋Š” 2 ์ด์ƒ 20 ์ดํ•˜.
    2. orders ๋ฐฐ์—ด์˜ ๊ฐ ์›์†Œ๋Š” ํฌ๊ธฐ๊ฐ€ 2 ์ด์ƒ 10 ์ดํ•˜์ธ ๋ฌธ์ž์—ด.
      ๊ฐ ๋ฌธ์ž์—ด์€ ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ ์ค‘๋ณต์€ ํ—ˆ์šฉ ์•ˆํ•จ.
    3. course ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋Š” 1 ์ด์ƒ 10 ์ดํ•˜.
      course ๋ฐฐ์—ด์˜ ๊ฐ ์›์†Œ๋Š” 2 ์ด์ƒ 10 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜๊ฐ€ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ
    4. ์ •๋‹ต์€ ๊ฐ ์ฝ”์Šค์š”๋ฆฌ ๋ฉ”๋‰ด์˜ ๊ตฌ์„ฑ์„ ๋ฌธ์ž์—ด ํ˜•์‹์œผ๋กœ ๋ฐฐ์—ด์— ๋‹ด์•„ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ด์„œ return
      ๋ฐฐ์—ด์˜ ๊ฐ ์›์†Œ์— ์ €์žฅ๋œ ๋ฌธ์ž์—ด ๋˜ํ•œ ์•ŒํŒŒ๋ฒณ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ
      ๋งŒ์•ฝ ๊ฐ€์žฅ ๋งŽ์ด ํ•จ๊ป˜ ์ฃผ๋ฌธ๋œ ๋ฉ”๋‰ด ๊ตฌ์„ฑ์ด ์—ฌ๋Ÿฌ ๊ฐœ๋ผ๋ฉด, ๋ชจ๋‘ ๋ฐฐ์—ด์— ๋‹ด์•„ return
      ๋ฌด์กฐ๊ฑด return ํ•˜๋Š” ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ 1 ์ด์ƒ
    5. Python ์กฐํ•ฉ(combinations) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด ๊ตฌํ˜„
      dict๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋˜๋Š” ๋ฌธ์ œ.
      ์ •๋ ฌ์ด ์ค‘์š”ํ•œ ๋ฌธ์ œ.
    6. ๊ฐ ์†๋‹˜์€ ๋‹จํ’ˆ๋ฉ”๋‰ด๋ฅผ 2๊ฐœ ์ด์ƒ ์ฃผ๋ฌธํ•ด์•ผ ํ•œ๋‹ค.
    7. ์ตœ์†Œ 2๋ช… ์ด์ƒ์˜ ์†๋‹˜์œผ๋กœ๋ถ€ํ„ฐ ์ฃผ๋ฌธ๋œ ๋‹จํ’ˆ๋ฉ”๋‰ด ์กฐํ•ฉ์— ๋Œ€ํ•ด์„œ๋งŒ ์ฝ”์Šค์š”๋ฆฌ ๋ฉ”๋‰ด ํ›„๋ณด์— ํฌํ•จ

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

    
    from itertools import combinations
    
    
    def solution(orders, course):
        answer = []
        score = {}
        n = len(orders)
        for i in range(n):
            data = sorted(list(orders[i]))
            for j in course:
                for k in list(set(combinations(data, j))):
                    t = ''.join(k)
                    if t in score:
                        score[t] += 1
                    else:
                        score[t] = 1
    
        new_score = []
        for menu, num in score.items():
            if num > 1:
                new_score.append((menu, num))
    
        new_score.sort(key=lambda x: -x[1])
    
        for i in course:
            max_order = 0
            for c, num in new_score:
                if len(c) == i:
                    if max_order <= num:
                        max_order = num
                        answer.append(c)
                    else:
                        break
        answer.sort()
        return answer

    ๐Ÿ”– ์˜ˆ์ œ ๋ฐ ์‹คํ–‰๊ฒฐ๊ณผ

    ์˜ˆ์ œ

    print(solution(["ABCFG", "AC", "CDE", "ACDE", "BCFG", "ACDEH"], [2, 3, 4]))
    

    ์‹คํ–‰๊ฒฐ๊ณผ

    ["AC", "ACDE", "BCFG", "CDE"]
    

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

    1. ์ฃผ๋ฌธ์„ ๋ฐ›์€ orders ๋ฐฐ์—ด์„ ์ˆœ์ฐจ์ ์œผ๋กœ ์ˆœํšŒํ•  ๋ฐ˜๋ณต๋ฌธ.
    2. ์ˆœํšŒํ•˜๋ฉฐ ๋ฉ”๋‰ด๋“ค์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค์–ด ์ •๋ ฌ.
    3. ์ฝ”์Šค๋ฅผ ๋งŒ๋“ค ๋ฉ”๋‰ด ๊ฐœ์ˆ˜๋ฅผ ์ˆœํšŒํ•  ๋ฐ˜๋ณต๋ฌธ. << ์—ฌ๊ธฐ๊นŒ์ง€ 2์ค‘ ๋ฐ˜๋ณต๋ฌธ. j
    4. ์กฐํ•ฉ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด 3๋ฒˆ ๋ฐ˜๋ณต๋ฌธ์—์„œ ๋‚˜์˜ค๋Š” ์ฝ”์Šค์˜ ๋ฉ”๋‰ด๊ฐœ์ˆ˜๋งŒํผ ๋ฉ”๋‰ด๋ฅผ ๋ฝ‘์•„์„œ
      ์ค‘๋ณต์„ ์—†์•ค ๋’ค t๋ฌธ์ž์—ด์— ์ €์žฅ. (t๋Š” j ๊ฐœ๋ฅผ ๋ฝ‘์•„ ๋งŒ๋“  ๋ฉ”๋‰ด์˜ ์กฐํ•ฉ์ž…๋‹ˆ๋‹ค.)
      list(set(combinations(data, j)))
      # set()์€ ์ค‘๋ณต์„ ์—†์• ์ค๋‹ˆ๋‹ค!
    5. t ๊ฐ€ ์ ์ˆ˜ํŒ์— ์—†์œผ๋ฉด ์ƒˆ๋กœ ๋“ฑ๋ก์‹œ์ผœ 1์  ๋ถ€์—ฌ
      t ๊ฐ€ ์ ์ˆ˜ํŒ์— ์žˆ์œผ๋ฉด 1์  ๋ถ€์—ฌ
    6. ์ตœ์†Œ 2๋ช… ์ด์ƒ์˜ ์†๋‹˜์œผ๋กœ๋ถ€ํ„ฐ ์ฃผ๋ฌธ๋œ ๋‹จํ’ˆ๋ฉ”๋‰ด ์กฐํ•ฉ ๋งŒ ํ•ด๋‹นํ•˜๊ธฐ ๋•Œ๋ฌธ์—
      score์— ์ €์žฅ๋œ ์ ์ˆ˜๊ฐ€ 1์  ์ด์ƒ์ด๋ผ๋ฉด new_score์— (๋ฉ”๋‰ด, ์ ์ˆ˜) ํ˜•์‹์œผ๋กœ ์ €์žฅ ํ›„ ์ ์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ
      """
      # lambda ์„ค๋ช…
      x ๋ฅผ x[1] ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ• ๊ฑด๋ฐ, -๊ฐ€ ๋ถ™์œผ๋ฉด ์—ญ์ˆœ์ž…๋‹ˆ๋‹ค.
      """ 
      new_score.sort(key=lambda x: -x[1])
    7. ๋งˆ์ง€๋ง‰์œผ๋กœ ์ฝ”์Šค์š”๋ฆฌ๋ฅผ ๊ตฌ์„ฑํ•  ๋ฉ”๋‰ด ๊ฐœ์ˆ˜๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ new_score์— ๋„ฃ์€ ์ฝ”์Šค๋ฉ”๋‰ด ํ›„๋ณด๋“ค์„ ๊ฒ€์‚ฌํ•˜๋ฉด์„œ
      ์ •๋‹ต์œผ๋กœ ๋ฐ˜ํ™˜ํ•  ๋ฐฐ์—ด answer์— ๋„ฃ์–ด์ฃผ๊ณ , ์ •๋ ฌ์„ ํ•œ ๋’ค ๋ฐ˜ํ™˜ ์‹œ์ผœ์ค€๋‹ค.

    ๐Ÿ’พ ๋Š๋‚€์ 

    • ์ •๋ ฌ์„ ํ•  ๋•Œ lambda๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•œ๊ฒŒ ๋„์›€์ด ๋˜์—ˆ๋‹ค.
    • combinations๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฒˆ๊ฑฐ๋กœ์šด ์ผ์ด ์ ์–ด์„œ ์ข‹์•˜๋‹ค.
      ์ด๋Ÿฐ ๊ฐ„ํŽธํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์•Œ๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ๋ณด๋‹ค ์‚ฌ์šฉํ•  ๋•Œ๋ฅผ ์•Œ๊ณ  ์žˆ๋‹ค๋Š”๊ฒŒ ๊ธฐ๋ถ„ ์ข‹์•˜๋‹ค.
    • ์นด์นด์˜ค ๊ธฐ์ถŸ๋ฌธ์ œ๋Š” ๋ฌธ์ œ๋„ ๊ธธ๊ณ  ์กฐ๊ฑด๋„ ์ž์„ธํ•˜๊ฒŒ ์จ์žˆ๋Š”๋ฐ ๋ˆˆ์— ์ž˜ ์•ˆ๋“ค์–ด์˜จ๋‹ค.
      ์•„์ง ๋ฌธ์ œ ์••์ถ•๋Šฅ๋ ฅ์ด ๋ถ€์กฑํ•œ ๊ฒƒ ๊ฐ™๋‹ค.
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.