https://school.programmers.co.kr/learn/courses/30/lessons/181916

 

๋ฌธ์ œ

 

๋ฌธ์ œ ์„ค๋ช…

1๋ถ€ํ„ฐ 6๊นŒ์ง€ ์ˆซ์ž๊ฐ€ ์ ํžŒ ์ฃผ์‚ฌ์œ„๊ฐ€ ๋„ค ๊ฐœ ์žˆ์Šต๋‹ˆ๋‹ค. ๋„ค ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ ธ์„ ๋•Œ ๋‚˜์˜จ ์ˆซ์ž์— ๋”ฐ๋ผ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ ์ˆ˜๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค.

๋„ค ์ฃผ์‚ฌ์œ„์—์„œ ๋‚˜์˜จ ์ˆซ์ž๊ฐ€ ๋ชจ๋‘ p๋กœ ๊ฐ™๋‹ค๋ฉด 1111 × p์ ์„ ์–ป์Šต๋‹ˆ๋‹ค.
์„ธ ์ฃผ์‚ฌ์œ„์—์„œ ๋‚˜์˜จ ์ˆซ์ž๊ฐ€ p๋กœ ๊ฐ™๊ณ  ๋‚˜๋จธ์ง€ ๋‹ค๋ฅธ ์ฃผ์‚ฌ์œ„์—์„œ ๋‚˜์˜จ ์ˆซ์ž๊ฐ€ q(p ≠ q)๋ผ๋ฉด (10 × p + q)2 ์ ์„ ์–ป์Šต๋‹ˆ๋‹ค.
์ฃผ์‚ฌ์œ„๊ฐ€ ๋‘ ๊ฐœ์”ฉ ๊ฐ™์€ ๊ฐ’์ด ๋‚˜์˜ค๊ณ , ๋‚˜์˜จ ์ˆซ์ž๋ฅผ ๊ฐ๊ฐ p, q(p ≠ q)๋ผ๊ณ  ํ•œ๋‹ค๋ฉด (p + q) × |p - q|์ ์„ ์–ป์Šต๋‹ˆ๋‹ค.
์–ด๋Š ๋‘ ์ฃผ์‚ฌ์œ„์—์„œ ๋‚˜์˜จ ์ˆซ์ž๊ฐ€ p๋กœ ๊ฐ™๊ณ  ๋‚˜๋จธ์ง€ ๋‘ ์ฃผ์‚ฌ์œ„์—์„œ ๋‚˜์˜จ ์ˆซ์ž๊ฐ€ ๊ฐ๊ฐ p์™€ ๋‹ค๋ฅธ q, r(q ≠ r)์ด๋ผ๋ฉด q × r์ ์„ ์–ป์Šต๋‹ˆ๋‹ค.
๋„ค ์ฃผ์‚ฌ์œ„์— ์ ํžŒ ์ˆซ์ž๊ฐ€ ๋ชจ๋‘ ๋‹ค๋ฅด๋‹ค๋ฉด ๋‚˜์˜จ ์ˆซ์ž ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ์ˆซ์ž ๋งŒํผ์˜ ์ ์ˆ˜๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค.
๋„ค ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ ธ์„ ๋•Œ ๋‚˜์˜จ ์ˆซ์ž๊ฐ€ ์ •์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜ a, b, c, d๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์–ป๋Š” ์ ์ˆ˜๋ฅผ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”.

 

์ œํ•œ ์‚ฌํ•ญ

  • a, b, c, d๋Š” 1 ์ด์ƒ 6 ์ดํ•˜์˜ ์ •์ˆ˜์ž…๋‹ˆ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ

a b c d result
2 2 2 2 2222
4 1 4 4 1681
6 3 3 6 27
2 5 2 6 30
6 4 2 5 2

 

 

์†Œ์Šค ์ฝ”๋“œ
# 1
def solution(a, b, c, d):
    answer = 0
    origin = [a,b,c,d]
    dice = list(set([a,b,c,d]))    
    
    # ๋ชจ๋‘ ๊ฐ™์€ ๊ฒฝ์šฐ
    if len(dice) == 1:    
        answer = dice[0] * 1111
        
    # 3๊ฐœ๊ฐ€ ๊ฐ™๊ฑฐ๋‚˜ 2๊ฐœ์”ฉ ๊ฐ™์€ ๊ฒฝ์šฐ
    elif len(dice) == 2:    
        # 3๊ฐœ๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ
        if max([origin.count(num) for num in dice]) == 3:
            p = max(origin, key=origin.count)
            q = min(origin, key=origin.count)
            answer = (10 * p + q)**2
        # 2๊ฐœ์”ฉ ๊ฐ™์€ ๊ฒฝ์šฐ
        else:
            answer = ((dice[0] + dice[1]) * abs(dice[0] - dice[1])) 
            
    # 2๊ฐœ๊ฐ€ ๊ฐ™๊ณ  ๋‚˜๋จธ์ง€๋Š” ๋‹ค๋ฅธ ๊ฒฝ์šฐ
    elif len(dice) == 3:
        for num in dice:
            if origin.count(num) == 2:
                dice.remove(num)
        answer = dice[0]*dice[1]
        
    # ๋ชจ๋‘ ๋‹ค๋ฅธ ๊ฒฝ์šฐ
    else:
        answer = min(dice)                
    
    return answer

 

ํ’€์ด # 1

  • a, b, c, d๊ฐ€ ๋ชจ๋‘ ์กด์žฌํ•˜๋Š” ๋ฆฌ์ŠคํŠธ = origin / ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ๋ฆฌ์ŠคํŠธ = dice
  • dice์— ์›์†Œ๊ฐ€ 1๊ฐœ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ a, b, c, d๊ฐ€ ๋ชจ๋‘ ๊ฐ™๋‹ค๋Š” ์˜๋ฏธ
  • dice์— ์›์†Œ๊ฐ€ 2๊ฐœ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ๋Š” 3๊ฐœ๊ฐ€ ๊ฐ™๊ฑฐ๋‚˜ 2๊ฐœ๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ
    • dice์˜ ์›์†Œ๊ฐ€ origin์— 3๊ฐœ ์žˆ์œผ๋ฉด origin.count ๊ธฐ์ค€์œผ๋กœ ์ตœ๋Œ“๊ฐ’=p / ์ตœ์†Ÿ๊ฐ’=q
    • ์œ„์˜ ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด 2๊ฐœ์”ฉ ๊ฐ™์€ ๊ฒฝ์šฐ
  • dice์— ์›์†Œ๊ฐ€ 3๊ฐœ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ๋Š” 2๊ฐœ๋Š” ๊ฐ™๊ณ  ๋‚˜๋จธ์ง€๋Š” ๋‹ค๋ฅธ ๊ฒฝ์šฐ
    • ๊ฐ™์€ 2๊ฐœ์˜ ์›์†Œ๋ฅผ ๋บด๊ณ  ๋‚˜๋จธ์ง€์˜ ๊ณฑ
  • ์œ„์˜ ๊ฒฝ์šฐ์— ํฌํ•จ๋˜์ง€ ์•Š์œผ๋ฉด a, b, c, d๊ฐ€ ๋ชจ๋‘ ๋‹ค๋ฅธ ๊ฒฝ์šฐ

 

# 2
def solution(a, b, c, d):
    answer = 0
    dice = [a,b,c,d]
    cnt = [dice.count(num) for num in dice]
    
    # ๋ชจ๋‘ ๊ฐ™์€ ๊ฒฝ์šฐ
    if max(cnt) == 4:
        answer = a * 1111
        
    # 3๊ฐœ๊ฐ€ ๊ฐ™๊ณ  ๋‚˜๋จธ์ง€ 1๊ฐœ๊ฐ€ ๋‹ค๋ฅธ ๊ฒฝ์šฐ
    elif max(cnt) == 3:
        answer = (10 * dice[cnt.index(3)] + dice[cnt.index(1)])**2
        
    # 2๊ฐœ๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ
    elif max(cnt) == 2:
        # 2๊ฐœ๊ฐ€ ๊ฐ™๊ณ  ๋‚˜๋จธ์ง€ 2๊ฐœ๋Š” ๋‹ค๋ฅธ ๊ฒฝ์šฐ
        if min(cnt) == 1:
            answer = 1
            answer = eval('*'.join([str(dice[i]) for i, c in enumerate(cnt) if c == 1])) 
        # 2๊ฐœ์”ฉ ๊ฐ™์€ ๊ฒฝ์šฐ
        else:
            answer = (max(dice) + min(dice)) * abs(max(dice) - min(dice))
    
    # ๋ชจ๋‘ ๋‹ค๋ฅธ ๊ฒฝ์šฐ
    else:
        answer = min(dice)
    
    return answer

 

ํ’€์ด #2

  • ์ฃผ์‚ฌ์œ„์—์„œ ๋‚˜์˜จ ์ˆซ์ž ๋ฆฌ์ŠคํŠธ = dice / ๊ฐ ์ˆซ์ž์˜ ๊ฐฏ์ˆ˜ ๋ฆฌ์ŠคํŠธ = cnt
  • ๊ฐฏ์ˆ˜์˜ ์ตœ๋Œ“๊ฐ’์ด 4์ธ ๊ฒฝ์šฐ๋Š” ๋ชจ๋‘ ๊ฐ™๋‹ค๋Š” ์˜๋ฏธ
  • ๊ฐฏ์ˆ˜์˜ ์ตœ๋Œ“๊ฐ’์ด 3์ธ ๊ฒฝ์šฐ๋Š” 3๊ฐœ๊ฐ€ ๊ฐ™๊ณ  ๋‚˜๋จธ์ง€ 1๊ฐœ๊ฐ€ ๋‹ค๋ฅด๋‹ค๋Š” ์˜๋ฏธ
  • ๊ฐฏ์ˆ˜์˜ ์ตœ๋Œ“๊ฐ’์ด 2์ธ ๊ฒฝ์šฐ์—๋Š” 2๊ฐ€์ง€ ์ƒํ™ฉ ์กด์žฌ
    • 2๊ฐœ๊ฐ€ ๊ฐ™๊ณ  ๋‚˜๋จธ์ง€ 2๊ฐœ๊ฐ€ ๋‹ค๋ฅธ ๊ฒฝ์šฐ์—๋Š” cnt๊ฐ€ 1์ธ ๊ฐ’๋“ค์˜ ๊ณฑ
    • 2๊ฐœ์”ฉ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” dice์˜ ์ตœ๋Œ“๊ฐ’ ์ตœ์†Ÿ๊ฐ’์œผ๋กœ ๊ฐ ์ˆซ์ž๋ฅผ ๊ตฌ๋ถ„
  • ์œ„์˜ ๊ฒฝ์šฐ์— ํฌํ•จ๋˜์ง€ ์•Š์œผ๋ฉด a, b, c, d๊ฐ€ ๋ชจ๋‘ ๋‹ค๋ฅธ ๊ฒฝ์šฐ

 

 

๋จธ๋ฆฌ ์‹ธ๋งค๋‹ค๊ฐ€ ์ฒซ ๋ฒˆ์งธ ํ’€์ด๋„ ๊ฒจ์šฐ ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ๋‘ ๋ฒˆ์งธ ํ’€์ด ๋„ˆ๋ฌด ์‹ ๋ฐ•ํ•˜๋‹ค. ํ‘ธ์‹  ๋ถ„ ๋„ˆ๋ฌด ๋Œ€๋‹จํ•ด์š”..๊ฐ™์€ ์ˆซ์ž์˜ ๊ฐฏ์ˆ˜๋ฅผ ์–ด๋–ป๊ฒŒ ๊ตฌ๋ถ„ํ•˜๋Š”์ง€ ์ƒ๊ฐํ•˜๊ธฐ๊ฐ€ ์–ด๋ ค์› ๋˜ ๋ฌธ์ œ์˜€๋‹ค.๐ŸŽฒ๐ŸŽฒ

+ Recent posts