ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Programmers] ํ›„๋ณดํ‚ค with Python
    PS 2021. 11. 27. 21:30
    728x90
    ๋ฐ˜์‘ํ˜•

    ๐Ÿ“Œ Programmers - [ํ›„๋ณดํ‚ค]

    ๐Ÿ’ก ์กฐ๊ฑด

    1. relation ์€ 2์ฐจ์› ๋ฌธ์ž์—ด ๋ฐฐ์—ด์ด๋‹ค.
      1 <= relation ์˜ ์ปฌ๋Ÿผ์˜ ๊ธธ์ด <= 8
      1 <= relation ์˜ ๋กœ์šฐ์˜ ๊ธธ์ด <= 20
      1 <= relation ์˜ ๋ชจ๋“  ๋ฌธ์ž์—ด์˜ ๊ธธ์ด <= 8, ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž์™€ ์ˆซ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.
      ์ค‘๋ณต๋˜๋Š” ํŠœํ”Œ์€ ์—†๋‹ค.

    2. ํ•™์ƒ๋“ค์˜ ์ธ์ ์‚ฌํ•ญ์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ํ›„๋ณด ํ‚ค์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋ผ.
      ์ฆ‰, ํ•™์ƒ๋“ค์„ ๊ตฌ๋ณ„ํ•  ์ˆ˜ ์žˆ๋Š” ์œ ์ผ์„ฑ๊ณผ ์ตœ์†Œ์„ฑ์„ ์ง€ํ‚ค๋Š” ํ‚ค์˜ ์กฐํ•ฉ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋ฉด ๋œ๋‹ค.

    3. ์กฐํ•ฉ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ

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

    from itertools import combinations
    
    
    def solution(relation):
        col = len(relation[0])
        row = len(relation)
    
        candidates = []
    
        for i in range(1, col + 1):
            candidates.extend(combinations(range(col), i))
    
        unique = []
        for candi in candidates:
            tmp = [tuple([item[i] for i in candi]) for item in relation]
            if len(set(tmp)) == row:
                unique.append(candi)
    
        answer = set(unique)
        for i in range(len(unique)):
            for j in range(i + 1, len(unique)):
                if len(unique[i]) == len(set(unique[i]) & set(unique[j])):
                    answer.discard(unique[j])
    
        return len(answer)

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

    ์˜ˆ์ œ

    [["100","ryan","music","2"],["200","apeach","math","2"],["300","tube","computer","3"],["400","con","computer","4"],["500","muzi","music","3"],["600","apeach","music","2"]]

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

    2

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

    1. row์™€ column ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•ด ๋ณ€์ˆ˜๋กœ ๋‘”๋‹ค.
      ํ‚ค๊ฐ’์ด ๋  ์ˆ˜ ์žˆ๋Š” 1๊ฐœ๋ถ€ํ„ฐ column ์˜ ๊ธธ์ด์˜ ์กฐํ•ฉ์„ ๊ฐ๊ฐ ๊ตฌํ•ด์„œ candidates์— ์ €์žฅํ•œ๋‹ค.

    2. 1๋ฒˆ์—์„œ ๊ตฌํ•œ ์กฐํ•ฉ์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ํ•ด๋‹น๋˜๋Š” relation ์˜ ๋ฐ์ดํ„ฐ๋ฅผ tmp ์— ์ €์žฅํ•œ ํ›„
      set() ์œผ๋กœ ์ž๋ฃŒํ˜•์„ ๋ณ€๊ฒฝํ•˜์—ฌ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ํ›„, row์˜ ๊ฐœ์ˆ˜์™€ ๋น„๊ตํ•œ๋‹ค.
      ๋งŒ์•ฝ ๊ฐ™๋‹ค๋ฉด, ํ›„๋ณดํ‚ค๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๊ธฐ์— unique ๋ณ€์ˆ˜์— ์ €์žฅํ•œ๋‹ค
      ์ถ”์ถœํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์œ ์ผ์„ฑ์„ ๊ฐ€์ง€๋Š”์ง€์— ๋Œ€ํ•ด์„œ ๊ฒ€์‚ฌํ•˜๋Š” ๊ฒƒ.

    1. answer ๋ณ€์ˆ˜์— 3๋ฒˆ ์ž‘์—…์„ ํ†ตํ•ด ๋‚˜์˜จ unique ๋ณ€์ˆ˜์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ set() ์ž๋ฃŒํ˜•์œผ๋กœ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ๋’ค ์ €์žฅํ•œ๋‹ค.
      unique ๋ณ€์ˆ˜์˜ ๊ธธ์ด๋งŒํผ ์ˆœํšŒํ•˜๋ฉด์„œ (i)
      i ์ดํ›„์˜ ๊ฐ ํ‚ค์กฐํ•ฉ์— (j) i๊ฐ€ ํฌํ•จ์ด ๋˜์–ด ์žˆ๋‹ค๋ฉด, answer์—์„œ j๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.
      i๊ฐ€ j์— ํฌํ•จ์ด ๋œ๋‹ค๋ฉด, j๋Š” ์›์†Œ ํ•˜๋‚˜๊ฐ€ ๋น ์ ธ๋„ ์ตœ์†Œ์„ฑ์„ ๋งŒ์กฑํ•˜์ง€ ์•Š๊ธฐ์— ์ œ๊ฑฐํ•˜๋Š” ๊ฒƒ.

    2. answer์˜ ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜

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

    1. ์กฐํ•ฉ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ œ์—์„œ ์ œ์‹œํ•œ ์ตœ์†Œ์„ฑ๊ณผ ์œ ์ผ์„ฑ์„ ์ดํ•ดํ•˜๊ณ  ๋ฌธ์ œ๋ฅผ ํ’€์ดํ•ด์•ผํ–ˆ๋‹ค.
    2. ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด์˜ ํฌ๊ธฐ๊ฐ€ ์ž‘์•„์„œ ์™„์ „ํƒ์ƒ‰์„ ํ†ตํ•ด์„œ ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
    3. set() ์ž๋ฃŒ๊ตฌ์กฐ์˜ ์„ฑ์งˆ์„ ํ†ตํ•ด์„œ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ณ  ์žˆ๋‹ค๋ฉด ๋” ํŽธํ•˜๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์˜€๋‹ค.
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.