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

 

๋ฌธ์ œ

 

๋ฌธ์ œ ์„ค๋ช…

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ํŒ€์—์„œ๋Š” ๊ธฐ๋Šฅ ๊ฐœ์„  ์ž‘์—…์„ ์ˆ˜ํ–‰ ์ค‘์ž…๋‹ˆ๋‹ค. ๊ฐ ๊ธฐ๋Šฅ์€ ์ง„๋„๊ฐ€ 100%์ผ ๋•Œ ์„œ๋น„์Šค์— ๋ฐ˜์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๋˜, ๊ฐ ๊ธฐ๋Šฅ์˜ ๊ฐœ๋ฐœ์†๋„๋Š” ๋ชจ๋‘ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๋’ค์— ์žˆ๋Š” ๊ธฐ๋Šฅ์ด ์•ž์— ์žˆ๋Š” ๊ธฐ๋Šฅ๋ณด๋‹ค ๋จผ์ € ๊ฐœ๋ฐœ๋  ์ˆ˜ ์žˆ๊ณ , ์ด๋•Œ ๋’ค์— ์žˆ๋Š” ๊ธฐ๋Šฅ์€ ์•ž์— ์žˆ๋Š” ๊ธฐ๋Šฅ์ด ๋ฐฐํฌ๋  ๋•Œ ํ•จ๊ป˜ ๋ฐฐํฌ๋ฉ๋‹ˆ๋‹ค.
๋จผ์ € ๋ฐฐํฌ๋˜์–ด์•ผ ํ•˜๋Š” ์ˆœ์„œ๋Œ€๋กœ ์ž‘์—…์˜ ์ง„๋„๊ฐ€ ์ ํžŒ ์ •์ˆ˜ ๋ฐฐ์—ด progresses์™€ ๊ฐ ์ž‘์—…์˜ ๊ฐœ๋ฐœ ์†๋„๊ฐ€ ์ ํžŒ ์ •์ˆ˜ ๋ฐฐ์—ด speeds๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ ๊ฐ ๋ฐฐํฌ๋งˆ๋‹ค ๋ช‡ ๊ฐœ์˜ ๊ธฐ๋Šฅ์ด ๋ฐฐํฌ๋˜๋Š”์ง€๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜์„ธ์š”.

 

์ œํ•œ ์‚ฌํ•ญ

  • ์ž‘์—…์˜ ๊ฐœ์ˆ˜(progresses, speeds๋ฐฐ์—ด์˜ ๊ธธ์ด)๋Š” 100๊ฐœ ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • ์ž‘์—… ์ง„๋„๋Š” 100 ๋ฏธ๋งŒ์˜ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • ์ž‘์—… ์†๋„๋Š” 100 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • ๋ฐฐํฌ๋Š” ํ•˜๋ฃจ์— ํ•œ ๋ฒˆ๋งŒ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํ•˜๋ฃจ์˜ ๋์— ์ด๋ฃจ์–ด์ง„๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ง„๋„์œจ์ด 95%์ธ ์ž‘์—…์˜
    ๊ฐœ๋ฐœ ์†๋„๊ฐ€ ํ•˜๋ฃจ์— 4%๋ผ๋ฉด ๋ฐฐํฌ๋Š” 2์ผ ๋’ค์— ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ

progresses speeds return
[93, 30, 55] [1, 30, 5] [2, 1]
[95, 90, 99, 99, 80, 99] [1, 1, 1, 1, 1] [1, 3, 2]

 

 

์†Œ์Šค ์ฝ”๋“œ
# 1
import math

def solution(progresses, speeds):
    n = len(progresses)
    answers = []

    # ๊ฐ ์ž‘์—…์ด 100% ๋˜๊ธฐ ์œ„ํ•ด ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„
    times = [math.ceil((100-progresses[i])/speeds[i]) for i in range(n)]

    cnt = 1                 # ์™„๋ฃŒ๋œ ์ž‘์—…์˜ ์ˆ˜
    max_time = times[0]     # ์ฒซ ๋ฒˆ์งธ ์ž‘์—…์˜ ์‹œ๊ฐ„์„ max_time์œผ๋กœ ์ง€์ •

    # ์‹œ๊ฐ„ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋Œ๋ฉด์„œ max_time๊ณผ ๋น„๊ต
    for i in range(1,n):
        if times[i] <= max_time:    # 1. ํ•„์š”ํ•œ ์‹œ๊ฐ„์ด max_time๋ณด๋‹ค ์ ์œผ๋ฉด
            cnt += 1                #    ์ž‘์—…์ด ์™„๋ฃŒ๋œ ๊ฒƒ์ด๋ฏ€๋กœ ์ž‘์—…์˜ ์ˆ˜ +1
        else:                       # 2. ํ•„์š”ํ•œ ์‹œ๊ฐ„์ด max_time๋ณด๋‹ค ๋งŽ์œผ๋ฉด
            answers.append(cnt)     #    ํ˜„์žฌ๊นŒ์ง€ ์™„๋ฃŒํ•œ ์ž‘์—…์˜ ์ˆ˜๋ฅผ answers์— ์ถ”๊ฐ€
            max_time = times[i]     #    max_time์„ ํ˜„์žฌ ์ž‘์—…์˜ ๋‚จ์€์‹œ๊ฐ„์œผ๋กœ ๋ณ€๊ฒฝ
            cnt = 1                 #    ์ž‘์—…์˜ ์ˆ˜ ์ดˆ๊ธฐํ™”
    
    answers.append(cnt)             # 3. ๋งˆ์ง€๋ง‰ ์ž‘์—…์ด ์ถ”๊ฐ€๋˜์ง€ ์•Š๊ณ  for๋ฌธ ์ข…๋ฃŒ๋˜์—ˆ์œผ๋ฏ€๋กœ ๋งˆ์ง€๋ง‰ ์ž‘์—… ์ˆ˜ ์ถ”๊ฐ€
        
    return answers

 

ํ’€์ด #1

  • ์ฃผ์–ด์ง„ ์ž‘์—…์˜ ์ˆ˜๋ฅผ n์œผ๋กœ ์ •์˜
  • ์•„๋ž˜์˜ ๊ณต์‹์„ ์ฐธ๊ณ ํ•˜์—ฌ ๊ฐ ์ž‘์—…์ด ์™„๋ฃŒ๋˜๋ ค๋ฉด ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์„ times ๋ฆฌ์ŠคํŠธ๋กœ ์ƒ์„ฑ
    • progresses(ํ˜„์žฌ ์™„๋ฃŒ๋œ ์ž‘์—…์˜ ์ •๋„) + (speeds(์ž‘์—…์˜ ์†๋„) * times(์ž‘์—… ์‹œ๊ฐ„) = 100%
  • cnt : ์™„๋ฃŒ๋œ ์ž‘์—…์˜ ์ˆ˜ / max_time : ์•ž๋ถ€๋ถ„ ์ž‘์—…์ด ์™„๋ฃŒ๋˜๋ ค๋ฉด ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„
  • ํ˜„์žฌ ์ž‘์—…์˜ ๋‚จ์€ ์‹œ๊ฐ„(times[i])๊ณผ ๊ธฐ์ค€์ด ๋˜๋Š” ์‹œ๊ฐ„(max_time)์„ ๋น„๊ต
    • times[i] ๊ฐ€ max_time๋ณด๋‹ค ์ ๋‹ค๋ฉด ํ˜„์žฌ์˜ ์ž‘์—…์ด ์™„๋ฃŒ๋œ ๊ฒƒ์ด๋ฏ€๋กœ cnt + 1
    • times[i]๊ฐ€ max_time๋ณด๋‹ค ๋งŽ๋‹ค๋ฉด ์ž‘์—…์ด ์™„๋ฃŒ๋˜๊ธฐ๊นŒ์ง€ ์‹œ๊ฐ„์ด ๋” ํ•„์š”ํ•œ ๊ฒƒ
      • ํ˜„์žฌ๊นŒ์ง€ ์™„๋ฃŒ๋œ ์ž‘์—…์˜ ์ˆ˜๋ฅผ answers์— ์ถ”๊ฐ€
      • ๊ธฐ์ค€์ด ๋˜๋Š” ์‹œ๊ฐ„(max_time)์„ ํ˜„์žฌ ์‹œ๊ฐ„์œผ๋กœ ๋ฐ”๊พธ๊ณ  cnt๋„ ์ดˆ๊ธฐํ™”

 

# 2
def solution(progresses, speeds):
    answers = []
    time = 0    # ์ž‘์—…์ด ํ•„์š”ํ•œ ์‹œ๊ฐ„
    cnt = 0     # ์™„๋ฃŒ๋œ ์ž‘์—…์˜ ์ˆ˜

    while len(progresses) > 0:
    	# 1. ์ง„ํ–‰ ์ƒํ™ฉ์ด 100 ์ด์ƒ์ด ๋œ ๊ฒฝ์šฐ
        if(progresses[0] + time * speeds[0]) >= 100:
            progresses.pop(0)   # ์ž‘์—…์ด ์™„๋ฃŒ๋œ ๊ฒƒ์ด๋ฏ€๋กœ progresses์—์„œ ํ•ด๋‹น ์ž‘์—… ์ œ๊ฑฐ
            speeds.pop(0)       # ์ž‘์—…๊ณผ ์†๋„์˜ ์ธ๋ฑ์Šค๋ฅผ ๋งž์ถ”์–ด์•ผ ํ•˜๋ฏ€๋กœ speeds์—์„œ๋„ ์ œ๊ฑฐ
            cnt += 1            # ์ž‘์—…์˜ ์ˆ˜ + 1
        # 2. ์ง„ํ–‰ ์ƒํ™ฉ์ด 100 ๋ฏธ๋งŒ์ธ ๊ฒฝ์šฐ
        else:
            if cnt > 0:                 # ์™„๋ฃŒ๋œ ์ž‘์—…์ด ์žˆ๋‹ค๋ฉด
                answers.append(cnt)     # ํ˜„์žฌ๊นŒ์ง€ ์™„๋ฃŒํ•œ ์ž‘์—…์„ answers์— ์ถ”๊ฐ€
                cnt = 0                 # ์ž‘์—…์˜ ์ˆ˜ ์ดˆ๊ธฐํ™”
            else:                       # ์™„๋ฃŒ๋œ ์ž‘์—…์ด ์—†๋‹ค๋ฉด
                time += 1               # ํ•„์š”ํ•œ ์‹œ๊ฐ„์„ ์ถ”๊ฐ€
    
    # 3. ๋งˆ์ง€๋ง‰ ์ž‘์—…์ด ์ถ”๊ฐ€๋˜์ง€ ์•Š๊ณ  while๋ฌธ ์ข…๋ฃŒ๋˜์—ˆ์œผ๋ฏ€๋กœ ๋งˆ์ง€๋ง‰ ์ž‘์—… ์ˆ˜ ์ถ”๊ฐ€
    answers.append(cnt)

    return answers

 

ํ’€์ด #2

  • ์ž‘์—…์ด ํ•„์š”ํ•œ ์‹œ๊ฐ„๊ณผ ์™„๋ฃŒ๋œ ์ž‘์—…์˜ ์ˆ˜๋ฅผ ๊ฐ๊ฐ time, cnt๋กœ ์ •์˜
  • ์ง„ํ–‰ ์ค‘์ธ ์ž‘์—…์ด ์žˆ๋Š” ๋ฆฌ์ŠคํŠธ(progresses)์˜ ๊ธธ์ด๊ฐ€ 0๋ณด๋‹ค ํฌ๋ฉด ์•„๋ž˜์˜ ๋‚ด์šฉ์„ ๋ฐ˜๋ณตํ•ด์„œ ์ง„ํ–‰
    • ์•„๋ž˜์˜ ๊ณต์‹์ด 100์ด์ƒ์ด๋ฉด ์ž‘์—…์ด ์™„๋ฃŒ๋œ ๊ฒƒ์ด๋ฏ€๋กœ progresses์™€ speed์—์„œ ํ•ด๋‹น ์ž‘์—… ์ œ๊ฑฐ
      • progresses(ํ˜„์žฌ ์™„๋ฃŒ๋œ ์ž‘์—…์˜ ์ •๋„) + (speeds(์ž‘์—…์˜ ์†๋„) * times(์ž‘์—… ์‹œ๊ฐ„) = 100%
      • ์™„๋ฃŒ๋œ ์ž‘์—…์˜ ์ˆ˜ cnt + 1
    • cnt๊ฐ€ 0๋ณด๋‹ค ํฌ๋‹ค๋ฉด ์™„๋ฃŒ๋œ ์ž‘์—…์ด ์žˆ๋Š” ๊ฒƒ์ด๋ฏ€๋กœ answers์— ์™„๋ฃŒ๋œ ์ž‘์—…์˜ ์ˆ˜ ์ถ”๊ฐ€ํ•˜๊ณ  ์ดˆ๊ธฐํ™”
    • ์™„๋ฃŒ๋œ ์ž‘์—…์ด ์—†๋‹ค๋ฉด ์‹œ๊ฐ„์ด ๋” ํ•„์š”ํ•œ ๊ฒƒ์ด๋ฏ€๋กœ ์‹œ๊ฐ„์„ ์ถ”๊ฐ€

 

์‰ฝ๊ฒŒ ํ’€๋ฆด ๊ฒƒ ๊ฐ™์•˜๋Š”๋ฐ ์€๊ทผํžˆ ํ’€๊ธฐ ์–ด๋ ค์šด ๋ฌธ์ œ์˜€๋‹ค.....๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์„ ๊ธฐ์ค€์œผ๋กœ ๋‘๊ณ  ๋น„๊ตํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ์ง„ํ–‰์ƒํ™ฉ์„ ํŒ๋‹จํ•ด์„œ ๋น„๊ตํ•˜๋Š” 2๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์–ด์„œ ํŽธํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค.
 

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

 

๋ฌธ์ œ

 

๋ฌธ์ œ ์„ค๋ช…

์ดˆ ๋‹จ์œ„๋กœ ๊ธฐ๋ก๋œ ์ฃผ์‹๊ฐ€๊ฒฉ์ด ๋‹ด๊ธด ๋ฐฐ์—ด prices๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๊ฐ€๊ฒฉ์ด ๋–จ์–ด์ง€์ง€ ์•Š์€ ๊ธฐ๊ฐ„์€ ๋ช‡ ์ดˆ์ธ์ง€๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜์„ธ์š”.

 

 

์ œํ•œ ์‚ฌํ•ญ

  • prices์˜ ๊ฐ ๊ฐ€๊ฒฉ์€ 1 ์ด์ƒ 10,000 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • prices์˜ ๊ธธ์ด๋Š” 2 ์ด์ƒ 100,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ

prices return
[1, 2, 3, 2, 3] [4, 3, 1, 1, 0]

 

 

์†Œ์Šค ์ฝ”๋“œ
# 1
def solution(prices):
    n = len(prices)
    answer = []

    for i in range(n):
        idx = 0
        for j in range(i+1,n):
            if prices[j] < prices[i]:   # ํ˜„์žฌ ์ฃผ์‹๊ฐ€๊ฒฉ๋ณด๋‹ค ๋–จ์–ด์ง„ ๊ฒฝ์šฐ
                idx = j                 # ๋–จ์–ด์ง„ ์‹œ์ ์„ idx์— ์ €์žฅํ•˜๊ณ  ์ข…๋ฃŒ
                break

        if idx==0:                      # ํ˜„์žฌ ์ฃผ์‹๊ฐ€๊ฒฉ์—์„œ ๋–จ์–ด์ง€์ง€ ์•Š์€ ๊ฒฝ์šฐ
            answer.append(n-i-1)        # ์ „์ฒด ๊ธธ์ด์—์„œ ํ˜„์žฌ ์ธ๋ฑ์Šค๋ฅผ ๋บ€ ๊ฐ’
        else:
            answer.append(idx-i)        # ๋–จ์–ด์ง„ ์‹œ์ ์—์„œ ํ˜„์žฌ ์ธ๋ฑ์Šค๋ฅผ ๋บธ ๊ฐ’

    return(answer)

 

ํ’€์ด # 1

  • ํ˜„์žฌ์˜ ์ฃผ์‹๊ฐ€๊ฒฉ์€ i๋ฒˆ์งธ์— ์žˆ๋Š” ๊ฐ’์ด๊ณ  ๋‚˜๋จธ์ง€ ์ฃผ์‹๊ฐ€๊ฒฉ์€ j๋ฒˆ์งธ์— ์žˆ๋Š” ๊ฐ’
  • ํ˜„์žฌ ์ฃผ์‹๊ฐ€๊ฒฉ๊ณผ ๋‚˜๋จธ์ง€ ์ฃผ์‹๊ฐ€๊ฒฉ์„ ๋น„๊ตํ•ด์„œ ๊ฐ€๊ฒฉ์ด ๋–จ์–ด์ง€๋Š” ์‹œ์ ์„ idx์— ์ €์žฅ
    • ๊ฐ€๊ฒฉ์ด ๋–จ์–ด์กŒ๋‹ค๋ฉด for๋ฌธ ๋น ์ ธ๋‚˜์˜ค๊ธฐ
  • idx๊ฐ€ 0์ด๋ฉด ๊ฐ€๊ฒฉ์ด ๋–จ์–ด์ง€์ง€ ์•Š์€ ๊ฒƒ์ด๋ฏ€๋กœ ์ „์ฒด๊ธธ์ด์—์„œ ํ˜„์žฌ ์ฃผ์‹๊ฐ€๊ฒฉ์˜ ์ธ๋ฑ์Šค ๋นผ๊ธฐ
  • idx๊ฐ€ 0์ด ์•„๋‹ˆ๋ฉด ๊ฐ€๊ฒฉ์ด ๋–จ์–ด์ง„ ์‹œ์ ์ด idx์ด๋ฏ€๋กœ ๋–จ์–ด์ง„ ์‹œ์ ์—์„œ ํ˜„์žฌ ์ธ๋ฑ์Šค ๋นผ๊ธฐ

 

# 2
def solution(prices):
    n = len(prices)
    answer = [0] * n     

    for i in range(n):
        for j in range(i+1, n):
            answer[i] += 1
            if prices[i] > prices[j]:   # ๊ฐ€๊ฒฉ์ด ๋–จ์–ด์ง„ ๊ฒฝ์šฐ
                break                   # for๋ฌธ ์ข…๋ฃŒ

    return answer

 

ํ’€์ด #2

  • ์ฃผ์‹๊ฐ€๊ฒฉ ๊ธธ์ด๋งŒํผ์˜ ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ
  • ํ˜„์žฌ ์ฃผ์‹๊ฐ€๊ฒฉ์€ i๋ฒˆ์งธ์— ์žˆ๋Š” ๊ฐ’์ด๊ณ  ๋‚˜๋จธ์ง€ ์ฃผ์‹๊ฐ€๊ฒฉ์€ j๋ฒˆ์งธ์— ์žˆ๋Š” ๊ฐ’
  • answer ๋ฆฌ์ŠคํŠธ์— 1์„ ๋”ํ•˜๊ณ  ๊ฐ€๊ฒฉ์ด ๋–จ์–ด์กŒ๋‹ค๋ฉด for๋ฌธ์„ ์ข…๋ฃŒ
    • ๊ฐ€๊ฒฉ์ด ๋–จ์–ด์ง€์ง€ ์•Š์•˜๋‹ค๋ฉด for๋ฌธ์œผ๋กœ ๋Œ๋ฉด์„œ +1 ์—ฐ์‚ฐ์„ ์ง„ํ–‰

 

์ฒ˜์Œ์—๋Š” ๋ฌธ์ œ ์ดํ•ด๊ฐ€ ์•ˆ๋์—ˆ๋Š”๋ฐ ๋‹จ์ˆœํ•˜๊ฒŒ ์ƒ๊ฐํ•ด๋ณด๋‹ˆ ํ˜„์žฌ ์ธ๋ฑ์Šค ๊ฐ’๊ณผ ๋‚˜๋จธ์ง€ ์ธ๋ฑ์Šค ๊ฐ’์„ ๋น„๊ตํ•ด์„œ ๊ฐ€๊ฒฉ์ด ๋–จ์–ด์ง€๊ธฐ ์ „๊นŒ์ง€ ์‹œ๊ฐ„์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค. ์ •ํ™•๋„์™€ ํšจ์œจ์„ฑ์„ ๋ชจ๋‘ ํ…Œ์ŠคํŠธํ•˜๋Š” ๋ฌธ์ œ๋ผ์„œ ์–ด๋ ต๊ฒŒ ์ƒ๊ฐํ–ˆ์—ˆ๋Š”๋ฐ ํ•„์š”์—†๋Š” ์—ฐ์‚ฐ์„ ์ง€์šฐ๊ณ  ์ฃผ์‹๊ฐ€๊ฒฉ์ด ๋–จ์–ด์ง€๋Š” ์ˆœ๊ฐ„๊นŒ์ง€๋งŒ ์—ฐ์‚ฐ์„ ์ง„ํ–‰ํ•˜๋‹ˆ ์˜์™ธ๋กœ ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

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

 

๋ฌธ์ œ

 

๋ฌธ์ œ ์„ค๋ช…

๋‹ค์Œ ๊ทœ์น™์„ ์ง€ํ‚ค๋Š” ๋ฌธ์ž์—ด์„ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ด๋ผ๊ณ  ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

 

  • (), [], {} ๋Š” ๋ชจ๋‘ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
  • ๋งŒ์•ฝ A๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ด๋ผ๋ฉด, (A), [A], {A} ๋„ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
    ์˜ˆ๋ฅผ ๋“ค์–ด, [] ๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ด๋ฏ€๋กœ, ([]) ๋„ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
  • ๋งŒ์•ฝ A, B๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ด๋ผ๋ฉด, AB ๋„ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
    ์˜ˆ๋ฅผ ๋“ค์–ด, {} ์™€ ([]) ๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ด๋ฏ€๋กœ, {}([]) ๋„ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.

 

๋Œ€๊ด„ํ˜ธ, ์ค‘๊ด„ํ˜ธ, ๊ทธ๋ฆฌ๊ณ  ์†Œ๊ด„ํ˜ธ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด s๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์ด s๋ฅผ ์™ผ์ชฝ์œผ๋กœ x (0 ≤ x < (s์˜ ๊ธธ์ด)) ์นธ๋งŒํผ ํšŒ์ „์‹œ์ผฐ์„ ๋•Œ s๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ด ๋˜๊ฒŒ ํ•˜๋Š” x์˜ ๊ฐœ์ˆ˜๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

 

์ œํ•œ ์‚ฌํ•ญ

  • s์˜ ๊ธธ์ด๋Š” 1์ด์ƒ 1,000์ดํ•˜์ž…๋‹ˆ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ

s result
"[](){}" 3
"}]()[{" 2
"[)(]" 0
"}}}" 0

 

 

์†Œ์Šค ์ฝ”๋“œ
def solution(s):
    answer = 0
    n = len(s)

    for i in range(n):
        stack = []  
        for j in range(n):
            # ๋ฌธ์ž์—ด ํšŒ์ „
            c = s[(i+j)%n]

            # ์—ด๋ฆฐ๊ด„ํ˜ธ์˜ ๊ฒฝ์šฐ
            if c=="(" or c=="[" or c=="{":
                stack.append(c)
            # ๋‹ซํžŒ๊ด„ํ˜ธ์˜ ๊ฒฝ์šฐ
            else:
                # ์Šคํƒ์ด ๋น„์–ด์žˆ๋‹ค๋ฉด 0์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์ข…๋ฃŒ
                if len(stack)==0:
                    stack.append(0)
                    break
                # ์Šคํƒ์˜ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์„ ํ™•์ธํ•˜๊ณ  ์ง์ด ๋งž๋‹ค๋ฉด ์ œ๊ฑฐ
                elif c==")" and stack[-1]=="(":     
                    stack.pop()
                elif c=="]" and stack[-1]=="[":
                    stack.pop()
                elif c=="}" and stack[-1]=="{":
                    stack.pop()
                # ์ง์ด ๋งž์ง€ ์•Š์€ ๊ฒฝ์šฐ 0์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์ข…๋ฃŒ
                else:
                    stack.append(0)
                    break
        
        # ์ค‘๊ฐ„์— ์ข…๋ฃŒ๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์Šคํƒ์€ ๋น„์–ด์žˆ์œผ๋ฏ€๋กœ +1
        if not stack:
            answer += 1

    return answer

 

ํ’€์ด

  • for๋ฌธ์— ๋”ฐ๋ผ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์„ ํšŒ์ „
  • ๋ฌธ์ž์—ด์ด ์—ด๋ฆฐ๊ด„ํ˜ธ์ธ ๊ฒฝ์šฐ ์Šคํƒ์— ๊ด„ํ˜ธ๋ฅผ ์ถ”๊ฐ€
  • ๋ฌธ์ž์—ด์ด ๋‹ซํžŒ๊ด„ํ˜ธ์ธ ๊ฒฝ์šฐ
    • ๋‹ซํžŒ๊ด„ํ˜ธ์ด๋ฉด์„œ ์Šคํƒ์ด ๋น„์–ด์žˆ๋‹ค๋ฉด ์•ž์— ์—ด๋ฆฐ๊ด„ํ˜ธ๊ฐ€ ์—†๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ์Šคํƒ์— 0์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์ข…๋ฃŒ
    • ๋‹ซํžŒ๊ด„ํ˜ธ์™€ ์Šคํƒ์— ๋งˆ์ง€๋ง‰ ๋ฌธ์ž์™€ ์ง ๋งž์ถ”๊ธฐ (์ง์ด ๋งž๋‹ค๋ฉด ์ œ๊ฑฐ)
    • ์ง์ด ๋งž์ง€ ์•Š๋‹ค๋ฉด ์Šคํƒ์— 0์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์ข…๋ฃŒ
  • ์ค‘๊ฐ„์— ์ข…๋ฃŒ๋˜์—ˆ๋‹ค๋ฉด ์Šคํƒ์— 0์ด ์žˆ์„ ๊ฒƒ์ด๊ณ  ์•„๋‹ˆ๋ผ๋ฉด ์Šคํƒ์ด ๋น„์–ด์žˆ์„ ๊ฒƒ์ด๋ฏ€๋กœ answer + 1

 

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

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

 

๋ฌธ์ œ

 

๋ฌธ์ œ ์„ค๋ช…

์„ ๋ฌผ์„ ์ง์ ‘ ์ „ํ•˜๊ธฐ ํž˜๋“ค ๋•Œ ์นด์นด์˜คํ†ก ์„ ๋ฌผํ•˜๊ธฐ ๊ธฐ๋Šฅ์„ ์ด์šฉํ•ด ์ถ•ํ•˜ ์„ ๋ฌผ์„ ๋ณด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹น์‹ ์˜ ์นœ๊ตฌ๋“ค์ด ์ด๋ฒˆ ๋‹ฌ๊นŒ์ง€ ์„ ๋ฌผ์„ ์ฃผ๊ณ ๋ฐ›์€ ๊ธฐ๋ก์„ ๋ฐ”ํƒ•์œผ๋กœ ๋‹ค์Œ ๋‹ฌ์— ๋ˆ„๊ฐ€ ์„ ๋ฌผ์„ ๋งŽ์ด ๋ฐ›์„์ง€ ์˜ˆ์ธกํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

๋‘ ์‚ฌ๋žŒ์ด ์„ ๋ฌผ์„ ์ฃผ๊ณ ๋ฐ›์€ ๊ธฐ๋ก์ด ์žˆ๋‹ค๋ฉด, ์ด๋ฒˆ ๋‹ฌ๊นŒ์ง€ ๋‘ ์‚ฌ๋žŒ ์‚ฌ์ด์— ๋” ๋งŽ์€ ์„ ๋ฌผ์„ ์ค€ ์‚ฌ๋žŒ์ด ๋‹ค์Œ ๋‹ฌ์— ์„ ๋ฌผ์„ ํ•˜๋‚˜ ๋ฐ›์Šต๋‹ˆ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด A๊ฐ€ B์—๊ฒŒ ์„ ๋ฌผ์„ 5๋ฒˆ ์คฌ๊ณ , B๊ฐ€ A์—๊ฒŒ ์„ ๋ฌผ์„ 3๋ฒˆ ์คฌ๋‹ค๋ฉด ๋‹ค์Œ ๋‹ฌ์—” A๊ฐ€ B์—๊ฒŒ ์„ ๋ฌผ์„ ํ•˜๋‚˜ ๋ฐ›์Šต๋‹ˆ๋‹ค.
๋‘ ์‚ฌ๋žŒ์ด ์„ ๋ฌผ์„ ์ฃผ๊ณ ๋ฐ›์€ ๊ธฐ๋ก์ด ํ•˜๋‚˜๋„ ์—†๊ฑฐ๋‚˜ ์ฃผ๊ณ ๋ฐ›์€ ์ˆ˜๊ฐ€ ๊ฐ™๋‹ค๋ฉด, ์„ ๋ฌผ ์ง€์ˆ˜๊ฐ€ ๋” ํฐ ์‚ฌ๋žŒ์ด ์„ ๋ฌผ ์ง€์ˆ˜๊ฐ€ ๋” ์ž‘์€ ์‚ฌ๋žŒ์—๊ฒŒ ์„ ๋ฌผ์„ ํ•˜๋‚˜ ๋ฐ›์Šต๋‹ˆ๋‹ค.
์„ ๋ฌผ ์ง€์ˆ˜๋Š” ์ด๋ฒˆ ๋‹ฌ๊นŒ์ง€ ์ž์‹ ์ด ์นœ๊ตฌ๋“ค์—๊ฒŒ ์ค€ ์„ ๋ฌผ์˜ ์ˆ˜์—์„œ ๋ฐ›์€ ์„ ๋ฌผ์˜ ์ˆ˜๋ฅผ ๋บ€ ๊ฐ’์ž…๋‹ˆ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด A๊ฐ€ ์นœ๊ตฌ๋“ค์—๊ฒŒ ์ค€ ์„ ๋ฌผ์ด 3๊ฐœ๊ณ  ๋ฐ›์€ ์„ ๋ฌผ์ด 10๊ฐœ๋ผ๋ฉด A์˜ ์„ ๋ฌผ ์ง€์ˆ˜๋Š” -7์ž…๋‹ˆ๋‹ค. B๊ฐ€ ์นœ๊ตฌ๋“ค์—๊ฒŒ ์ค€ ์„ ๋ฌผ์ด 3๊ฐœ๊ณ  ๋ฐ›์€ ์„ ๋ฌผ์ด 2๊ฐœ๋ผ๋ฉด B์˜ ์„ ๋ฌผ ์ง€์ˆ˜๋Š” 1์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ A์™€ B๊ฐ€ ์„ ๋ฌผ์„ ์ฃผ๊ณ ๋ฐ›์€ ์ ์ด ์—†๊ฑฐ๋‚˜ ์ •ํ™•ํžˆ ๊ฐ™์€ ์ˆ˜๋กœ ์„ ๋ฌผ์„ ์ฃผ๊ณ ๋ฐ›์•˜๋‹ค๋ฉด, ๋‹ค์Œ ๋‹ฌ์—” B๊ฐ€ A์—๊ฒŒ ์„ ๋ฌผ์„ ํ•˜๋‚˜ ๋ฐ›์Šต๋‹ˆ๋‹ค.
๋งŒ์•ฝ ๋‘ ์‚ฌ๋žŒ์˜ ์„ ๋ฌผ ์ง€์ˆ˜๋„ ๊ฐ™๋‹ค๋ฉด ๋‹ค์Œ ๋‹ฌ์— ์„ ๋ฌผ์„ ์ฃผ๊ณ ๋ฐ›์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
์œ„์—์„œ ์„ค๋ช…ํ•œ ๊ทœ์น™๋Œ€๋กœ ๋‹ค์Œ ๋‹ฌ์— ์„ ๋ฌผ์„ ์ฃผ๊ณ ๋ฐ›์„ ๋•Œ, ๋‹น์‹ ์€ ์„ ๋ฌผ์„ ๊ฐ€์žฅ ๋งŽ์ด ๋ฐ›์„ ์นœ๊ตฌ๊ฐ€ ๋ฐ›์„ ์„ ๋ฌผ์˜ ์ˆ˜๋ฅผ ์•Œ๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค.

์นœ๊ตฌ๋“ค์˜ ์ด๋ฆ„์„ ๋‹ด์€ 1์ฐจ์› ๋ฌธ์ž์—ด ๋ฐฐ์—ด friends ์ด๋ฒˆ ๋‹ฌ๊นŒ์ง€ ์นœ๊ตฌ๋“ค์ด ์ฃผ๊ณ ๋ฐ›์€ ์„ ๋ฌผ ๊ธฐ๋ก์„ ๋‹ด์€ 1์ฐจ์› ๋ฌธ์ž์—ด ๋ฐฐ์—ด gifts๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์ด๋•Œ, ๋‹ค์Œ๋‹ฌ์— ๊ฐ€์žฅ ๋งŽ์€ ์„ ๋ฌผ์„ ๋ฐ›๋Š” ์นœ๊ตฌ๊ฐ€ ๋ฐ›์„ ์„ ๋ฌผ์˜ ์ˆ˜๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด ์ฃผ์„ธ์š”.

 

์ œํ•œ ์‚ฌํ•ญ

  • 2 ≤ friends์˜ ๊ธธ์ด = ์นœ๊ตฌ๋“ค์˜ ์ˆ˜ ≤ 50
    • friends์˜ ์›์†Œ๋Š” ์นœ๊ตฌ์˜ ์ด๋ฆ„์„ ์˜๋ฏธํ•˜๋Š” ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๊ธธ์ด๊ฐ€ 10 ์ดํ•˜์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
    • ์ด๋ฆ„์ด ๊ฐ™์€ ์นœ๊ตฌ๋Š” ์—†์Šต๋‹ˆ๋‹ค.
  • 1 ≤ gifts์˜ ๊ธธ์ด ≤ 10,000
    • gifts์˜ ์›์†Œ๋Š” "A B"ํ˜•ํƒœ์˜ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
    • A๋Š” ์„ ๋ฌผ์„ ์ค€ ์นœ๊ตฌ์˜ ์ด๋ฆ„์„ B๋Š” ์„ ๋ฌผ์„ ๋ฐ›์€ ์นœ๊ตฌ์˜ ์ด๋ฆ„์„ ์˜๋ฏธํ•˜๋ฉฐ ๊ณต๋ฐฑ ํ•˜๋‚˜๋กœ ๊ตฌ๋ถ„๋ฉ๋‹ˆ๋‹ค.
    • A์™€ B๋Š” friends์˜ ์›์†Œ์ด๋ฉฐ A์™€ B๊ฐ€ ๊ฐ™์€ ์ด๋ฆ„์ธ ๊ฒฝ์šฐ๋Š” ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ

friends gifts result
["muzi", "ryan", "frodo", "neo"] ["muzi frodo", "muzi frodo", "ryan muzi", "ryan muzi", "ryan muzi", "frodo muzi", "frodo ryan", "neo muzi"] 2
["joy", "brad", "alessandro", "conan", "david"] ["alessandro brad", "alessandro joy", "alessandro conan", "david alessandro", "alessandro david"] 4
["a", "b", "c"] ["a b", "b a", "c a", "a c", "a c", "c a"] 0

 

 

์†Œ์Šค ์ฝ”๋“œ
# 1
def solution(friends, gifts):
    num = len(friends)

    # ์„ ๋ฌผ ์ฃผ๊ณ ๋ฐ›์€ ๊ธฐ๋ก ๋ฐ ์ ์ˆ˜ ์ƒ์„ฑ
    record = []
    score = [0]*num
    for name in friends:
        take_list = [0]*num
        for give_take in gifts:
            give, take = give_take.split(" ")
            if name == give:
                take_list[friends.index(take)] += 1
                score[friends.index(give)] += 1
            elif name == take:
                score[friends.index(take)] -= 1
        record.append(take_list)    

    # ๊ธฐ๋ก์„ ๋Œ๋ฉด์„œ ์„ ๋ฌผ์„ ๋” ๋งŽ์ด ์ค€ ์นœ๊ตฌ์˜ cnt ๋”ํ•˜๊ธฐ
    # ๊ธฐ๋ก์ด ์—†๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด ์„ ๋ฌผ์ง€์ˆ˜ score๋ฅผ ๋น„๊ต
    cnt = [0]*num
    for i in range(1,num):        
        for j in range(num-1):
            if i > j:
                if record[i][j] > record[j][i]:
                    cnt[i] += 1              
                elif record[i][j] < record[j][i]:
                    cnt[j] += 1
                elif i!=j and record[i][j] == record[j][i]:
                    if score[i] > score[j]:
                        cnt[i] += 1
                    elif score[i] < score[j]:
                        cnt[j] += 1     

    return max(cnt)

 

ํ’€์ด # 1

  • ์„ ๋ฌผ์„ ์ฃผ๊ณ ๋ฐ›์€ ๊ธฐ๋ก์ด record์™€ ์„ ๋ฌผ์ง€์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” score๋ฅผ ์ •์˜
  • friends ๋ฆฌ์ŠคํŠธ ๋Œ๋ฉด์„œ ์„ ๋ฌผ์ง€์ˆ˜๋ฅผ ๊ตฌํ•˜๊ณ  ์„ ๋ฌผ๊ธฐ๋ก์€ 2์ฐจ์› ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค๊ธฐ
    • friends ๋ฆฌ์ŠคํŠธ์˜ ์ด๋ฆ„์ด ์„ ๋ฌผ์„ ์ค€ ์ด๋ฆ„๊ณผ ๊ฐ™๋‹ค๋ฉด take_list์—์„œ ์„ ๋ฌผ์„ ๋ฐ›์€ ์นœ๊ตฌ ์ธ๋ฑ์Šค์— +1
    • ์„ ๋ฌผ์„ ์ค€ ๊ฒฝ์šฐ์— ์„ ๋ฌผ์ง€์ˆ˜์— +1 / ์„ ๋ฌผ์„ ๋ฐ›์€ ๊ฒฝ์šฐ์— ์„ ๋ฌผ์ง€์ˆ˜์— -1
  • ์„ ๋ฌผ์„ ์ฃผ๊ณ ๋ฐ›์€ 2์ฐจ์› ๋ฐฐ์—ด record๋ฅผ ๋Œ๋ฉด์„œ ๊ฐ ์นœ๊ตฌ๋“ค์ด ๋ฐ›์„ ์„ ๋ฌผ์˜ ๊ฐฏ์ˆ˜ ๊ณ„์‚ฐ
    • ์„ ๋ฌผ์„ ์ฃผ๊ณ ๋ฐ›์€ ์นœ๊ตฌ๊ฐ€ ๊ฐ™๊ฑฐ๋‚˜ ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€ ๊ฒฝ์šฐ๋Š” ์ œ์™ธํ•˜๊ธฐ ์œ„ํ•ด i>j ์กฐ๊ฑด ์ถ”๊ฐ€
  • ๋‹ค์Œ๋‹ฌ์— ๋ฐ›์„ ์„ ๋ฌผ์˜ ๊ฐฏ์ˆ˜๊ฐ€ ๋‹ด๊ธด cnt์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’ ๊ตฌํ•˜๊ธฐ

 

# 2
def solution(friends, gifts):
    record = {}
    for name1 in friends:
        # ์„ ๋ฌผ์ง€์ˆ˜์™€ ์„ ๋ฌผ๊ธฐ๋ก
        record[name1] = [0,{}]
        for name2 in friends:
            if name1 != name2:
                record[name1][1][name2] = 0

    for give_take in gifts:
        give, take = give_take.split(" ")

        record[give][0] += 1            # ์„ ๋ฌผ์ง€์ˆ˜ ๊ณ„์‚ฐ
        record[give][1][take] += 1      # ์„ ๋ฌผ์„ ์ค€ ๊ฒฝ์šฐ +1 

        record[take][0] -= 1            # ์„ ๋ฌผ์ง€์ˆ˜ ๊ณ„์‚ฐ
        record[take][1][give] -= 1      # ์„ ๋ฌผ์„ ๋ฐ›์€ ๊ฒฝ์šฐ -1


    answer = [0] * len(friends)
    for give_name, values in record.items():
        give_idx = friends.index(give_name)
        score = values[0]
        for take_name, cnt in values[1].items():
            # cnt๊ฐ€ 0๋ณด๋‹ค ํฌ๋ฉด give_name์ด ์„ ๋ฌผ์„ ๋” ๋งŽ์ด ์ค€ ๊ฒฝ์šฐ
            if cnt > 0:
                answer[give_idx] += 1
            # cnt๊ฐ€ 0์ด๋ฉด ๊ธฐ๋ก์ด ์—†๊ฑฐ๋‚˜ ์ฃผ๊ณ  ๋ฐ›์€ ์ˆ˜๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ
            elif cnt == 0:
                if score > record[take_name][0]:
                    answer[give_idx] += 1

    return max(answer)

 

ํ’€์ด #2

  • ์„ ๋ฌผ์ง€์ˆ˜์™€ ์„ ๋ฌผ๊ธฐ๋ก์„ ์ €์žฅํ•˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ record ์ •์˜
    • record์˜ ํ‚ค๋Š” ์„ ๋ฌผ์„ ์ค€ ์นœ๊ตฌ์˜ ์ด๋ฆ„
    • record์˜ ๊ฐ’์€ ์„ ๋ฌผ์ง€์ˆ˜์™€ {์„ ๋ฌผ์„ ๋ฐ›์€ ์นœ๊ตฌ:์„ ๋ฌผ์˜ ๊ฐฏ์ˆ˜}
  • gifts๋ฅผ ๋Œ๋ฉด์„œ ์„ ๋ฌผ์ง€์ˆ˜์™€ ์„ ๋ฌผ์˜ ๊ฐฏ์ˆ˜๋ฅผ ๊ณ„์‚ฐ
    • ์„ ๋ฌผ์„ ์ค€ ์นœ๊ตฌ์˜ ํ‚ค์—์„œ ์„ ๋ฌผ์„ ๋ฐ›์€ ์นœ๊ตฌ์—๊ฒŒ +1
    • ์„ ๋ฌผ์„ ๋ฐ›์€ ์นœ๊ตฌ์˜ ํ‚ค์—์„œ ์„ ๋ฌผ์„ ์ค€ ์นœ๊ตฌ์—๊ฒŒ -1
    • ์„ ๋ฌผ์„ ๋งŽ์ด ์ฃผ์—ˆ์„ ๊ฒฝ์šฐ์—๋Š” + / ์„ ๋ฌผ์„ ๋งŽ์ด ๋ฐ›์•˜์„ ๊ฒฝ์šฐ์—๋Š” -
  • record๋ฅผ ๋Œ๋ฉด์„œ ๋ฐ›์•„์•ผํ•˜๋Š” ์„ ๋ฌผ์˜ ๊ฐฏ์ˆ˜๋ฅผ answer์— ์ €์žฅ
    • record์˜ ํ‚ค๋Š” ์„ ๋ฌผ์„ ์ค€ ์นœ๊ตฌ์˜ ์ด๋ฆ„ / ๊ฐ’์€ ์„ ๋ฌผ์ง€์ˆ˜์™€ {์„ ๋ฌผ์„ ๋ฐ›์€ ์นœ๊ตฌ:์„ ๋ฌผ์˜ ๊ฐฏ์ˆ˜}
    • ์„ ๋ฌผ์„ ์ค€ ์นœ๊ตฌ์˜ ์ธ๋ฑ์Šค๋ฅผ give_index, ์„ ๋ฌผ์ง€์ˆ˜๋ฅผ score์— ์ €์žฅ
    • ์„ ๋ฌผ์„ ๋ฐ›์€ ์นœ๊ตฌ์˜ cnt๊ฐ€ 0๋ณด๋‹ค ํฌ๋ฉด ์„ ๋ฌผ์„ ๋งŽ์ด ์ค€ ๊ฒฝ์šฐ์ด๋ฏ€๋กœ answer + 1
    • cnt๊ฐ€ 0์ด๋ฉด ๊ธฐ๋ก์ด ์—†๊ฑฐ๋‚˜ ์ฃผ๊ณ  ๋ฐ›์€ ์ˆ˜๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ์ด๋ฏ€๋กœ ์„ ๋ฌผ์ง€์ˆ˜๋ฅผ ๋น„๊ต
  • ๋‹ค์Œ๋‹ฌ์— ๋ฐ›์„ ์„ ๋ฌผ์˜ ๊ฐฏ์ˆ˜๊ฐ€ ๋‹ด๊ธด answer์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’ ๊ตฌํ•˜๊ธฐ

 

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

 

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

 

๋ฌธ์ œ

 

๋ฌธ์ œ ์„ค๋ช…

์–ด๋–ค ๊ฒŒ์ž„์—๋Š” ๋ถ•๋Œ€ ๊ฐ๊ธฐ๋ผ๋Š” ๊ธฐ์ˆ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

๋ถ•๋Œ€ ๊ฐ๊ธฐ๋Š” t์ดˆ ๋™์•ˆ ๋ถ•๋Œ€๋ฅผ ๊ฐ์œผ๋ฉด์„œ 1์ดˆ๋งˆ๋‹ค x๋งŒํผ์˜ ์ฒด๋ ฅ์„ ํšŒ๋ณตํ•ฉ๋‹ˆ๋‹ค. t์ดˆ ์—ฐ์†์œผ๋กœ ๋ถ•๋Œ€๋ฅผ ๊ฐ๋Š” ๋ฐ ์„ฑ๊ณตํ•œ๋‹ค๋ฉด y๋งŒํผ์˜ ์ฒด๋ ฅ์„ ์ถ”๊ฐ€๋กœ ํšŒ๋ณตํ•ฉ๋‹ˆ๋‹ค. ๊ฒŒ์ž„ ์บ๋ฆญํ„ฐ์—๋Š” ์ตœ๋Œ€ ์ฒด๋ ฅ์ด ์กด์žฌํ•ด ํ˜„์žฌ ์ฒด๋ ฅ์ด ์ตœ๋Œ€ ์ฒด๋ ฅ๋ณด๋‹ค ์ปค์ง€๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๊ธฐ์ˆ ์„ ์“ฐ๋Š” ๋„์ค‘ ๋ชฌ์Šคํ„ฐ์—๊ฒŒ ๊ณต๊ฒฉ์„ ๋‹นํ•˜๋ฉด ๊ธฐ์ˆ ์ด ์ทจ์†Œ๋˜๊ณ , ๊ณต๊ฒฉ์„ ๋‹นํ•˜๋Š” ์ˆœ๊ฐ„์—๋Š” ์ฒด๋ ฅ์„ ํšŒ๋ณตํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋ชฌ์Šคํ„ฐ์—๊ฒŒ ๊ณต๊ฒฉ๋‹นํ•ด ๊ธฐ์ˆ ์ด ์ทจ์†Œ๋‹นํ•˜๊ฑฐ๋‚˜ ๊ธฐ์ˆ ์ด ๋๋‚˜๋ฉด ๊ทธ ์ฆ‰์‹œ ๋ถ•๋Œ€ ๊ฐ๊ธฐ๋ฅผ ๋‹ค์‹œ ์‚ฌ์šฉํ•˜๋ฉฐ, ์—ฐ์† ์„ฑ๊ณต ์‹œ๊ฐ„์ด 0์œผ๋กœ ์ดˆ๊ธฐํ™”๋ฉ๋‹ˆ๋‹ค.

๋ชฌ์Šคํ„ฐ์˜ ๊ณต๊ฒฉ์„ ๋ฐ›์œผ๋ฉด ์ •ํ•ด์ง„ ํ”ผํ•ด๋Ÿ‰๋งŒํผ ํ˜„์žฌ ์ฒด๋ ฅ์ด ์ค„์–ด๋“ญ๋‹ˆ๋‹ค. ์ด๋•Œ, ํ˜„์žฌ ์ฒด๋ ฅ์ด 0 ์ดํ•˜๊ฐ€ ๋˜๋ฉด ์บ๋ฆญํ„ฐ๊ฐ€ ์ฃฝ์œผ๋ฉฐ ๋” ์ด์ƒ ์ฒด๋ ฅ์„ ํšŒ๋ณตํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

๋‹น์‹ ์€ ๋ถ•๋Œ€๊ฐ๊ธฐ ๊ธฐ์ˆ ์˜ ์ •๋ณด, ์บ๋ฆญํ„ฐ๊ฐ€ ๊ฐ€์ง„ ์ตœ๋Œ€ ์ฒด๋ ฅ๊ณผ ๋ชฌ์Šคํ„ฐ์˜ ๊ณต๊ฒฉ ํŒจํ„ด์ด ์ฃผ์–ด์งˆ ๋•Œ ์บ๋ฆญํ„ฐ๊ฐ€ ๋๊นŒ์ง€ ์ƒ์กดํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค.

๋ถ•๋Œ€ ๊ฐ๊ธฐ ๊ธฐ์ˆ ์˜ ์‹œ์ „ ์‹œ๊ฐ„, 1์ดˆ๋‹น ํšŒ๋ณต๋Ÿ‰, ์ถ”๊ฐ€ ํšŒ๋ณต๋Ÿ‰์„ ๋‹ด์€ 1์ฐจ์› ์ •์ˆ˜ ๋ฐฐ์—ด bandage์™€ ์ตœ๋Œ€ ์ฒด๋ ฅ์„ ์˜๋ฏธํ•˜๋Š” ์ •์ˆ˜ health, ๋ชฌ์Šคํ„ฐ์˜ ๊ณต๊ฒฉ ์‹œ๊ฐ„๊ณผ ํ”ผํ•ด๋Ÿ‰์„ ๋‹ด์€ 2์ฐจ์› ์ •์ˆ˜ ๋ฐฐ์—ด attacks๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๋ชจ๋“  ๊ณต๊ฒฉ์ด ๋๋‚œ ์งํ›„ ๋‚จ์€ ์ฒด๋ ฅ์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด ์ฃผ์„ธ์š”. ๋งŒ์•ฝ ๋ชฌ์Šคํ„ฐ์˜ ๊ณต๊ฒฉ์„ ๋ฐ›๊ณ  ์บ๋ฆญํ„ฐ์˜ ์ฒด๋ ฅ์ด 0 ์ดํ•˜๊ฐ€ ๋˜์–ด ์ฃฝ๋Š”๋‹ค๋ฉด -1์„ return ํ•ด์ฃผ์„ธ์š”.

 

์ œํ•œ ์‚ฌํ•ญ

  • bandage๋Š” [์‹œ์ „ ์‹œ๊ฐ„, ์ดˆ๋‹น ํšŒ๋ณต๋Ÿ‰, ์ถ”๊ฐ€ ํšŒ๋ณต๋Ÿ‰] ํ˜•ํƒœ์˜ ๊ธธ์ด๊ฐ€ 3์ธ ์ •์ˆ˜ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
  • 1 ≤ ์‹œ์ „ ์‹œ๊ฐ„ = t ≤ 50
  • 1 ≤ ์ดˆ๋‹น ํšŒ๋ณต๋Ÿ‰ = x ≤ 100
  • 1 ≤ ์ถ”๊ฐ€ ํšŒ๋ณต๋Ÿ‰ = y ≤ 100
  • 1 ≤ health ≤ 1,000
  • 1 ≤ attacks์˜ ๊ธธ์ด ≤ 100
  • attacks[i]๋Š” [๊ณต๊ฒฉ ์‹œ๊ฐ„, ํ”ผํ•ด๋Ÿ‰] ํ˜•ํƒœ์˜ ๊ธธ์ด๊ฐ€ 2์ธ ์ •์ˆ˜ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
  • attacks๋Š” ๊ณต๊ฒฉ ์‹œ๊ฐ„์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ๋œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค.
  • attacks์˜ ๊ณต๊ฒฉ ์‹œ๊ฐ„์€ ๋ชจ๋‘ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.
  • 1 ≤ ๊ณต๊ฒฉ ์‹œ๊ฐ„ ≤ 1,000
  • 1 ≤ ํ”ผํ•ด๋Ÿ‰ ≤ 100

 

์ž…์ถœ๋ ฅ ์˜ˆ

bandage health attacks result
[5, 1, 5] 30 [[2, 10], [9, 15], [10, 5], [11, 5]] 5
[3, 2, 7] 20 [[1, 15], [5, 16], [8, 6]] -1
[4, 2, 7] 20 [[1, 15], [5, 16], [8, 6]] -1
[1, 1, 1] 5 [[1, 2], [3, 2]] 3

 

 

 

์†Œ์Šค ์ฝ”๋“œ
def solution(bandage, health, attacks):
    t, x, y = bandage
    max = health
    success = 0

    # ๊ณต๊ฒฉ ๋‹นํ•œ ์‹œ๊ฐ„ ๊ตฌํ•˜๊ธฐ
    attack_times = []
    for attack in attacks:
        attack_times.append(attack[0])

    # ๋งˆ์ง€๋ง‰ ๊ณต๊ฒฉ๊นŒ์ง€ ์ƒ์กด์—ฌ๋ถ€ ํ™•์ธ
    for now in range(1,attack_times[-1]+1):
        # ๊ณต๊ฒฉ ๋‹นํ•˜๋ฉด ์ฒด๋ ฅ๊ฐ์†Œ ๋ฐ ์—ฐ์†์„ฑ๊ณต ์ดˆ๊ธฐํ™”
        if now in attack_times:
            idx = attack_times.index(now)
            health -= attacks[idx][1]
            success = 0

            # ๊ณต๊ฒฉ ๋‹นํ•ด์„œ ์ฒด๋ ฅ์ด ๋ฐ”๋‹ฅ๋‚˜๋Š” ๊ฒฝ์šฐ
            if health <= 0:
                return -1
                break

        # ๊ณต๊ฒฉ ๋‹นํ•˜์ง€ ์•Š์œผ๋ฉด ๋ถ•๋Œ€์Šคํ‚ฌ
        else:
            # ์ฒด๋ ฅ์ด ์ตœ๋Œ€์น˜๋ณด๋‹ค ์ž‘์œผ๋ฉด ํšŒ๋ณต / ์ตœ๋Œ€์น˜์ด๊ฑฐ๋‚˜ ํฌ๋ฉด ์ตœ๋Œ€์น˜๋กœ ์ดˆ๊ธฐํ™”
            # ๋ชฌ์Šคํ„ฐ์˜ ๊ณต๊ฒฉ์„ ๋ฐ›์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ ์—ฐ์† ์„ฑ๊ณต
            if health < max:
                health += x
            else:
                health = max

            success += 1
            if success == t:
                health += y
                success = 0    

    return health

 

ํ’€์ด

  • ๋ถ•๋Œ€๊ฐ๊ธฐ ์Šคํ‚ฌ ๋ฆฌ์ŠคํŠธ์—์„œ ์‹œ์ „ ์‹œ๊ฐ„, ์ดˆ๋‹น ํšŒ๋ณต๋Ÿ‰, ์ถ”๊ฐ€ ํšŒ๋ณต๋Ÿ‰ ์„ค์ •
    • t = ์‹œ์ „ ์‹œ๊ฐ„ (๋ถ•๋Œ€๊ฐ๊ธฐ ์Šคํ‚ฌ์˜ ์ตœ๋Œ€ ์ง„ํ–‰์‹œ๊ฐ„)
    • x = ์ดˆ๋‹น ํšŒ๋ณต๋Ÿ‰ (1์ดˆ๋™์•ˆ ํšŒ๋ณต๋Ÿ‰)
    • y = ์ถ”๊ฐ€ ํšŒ๋ณต๋Ÿ‰ (t์‹œ๊ฐ„๋งŒํผ ๋ถ•๋Œ€๊ฐ๊ธฐ ์Šคํ‚ฌ์„ ์„ฑ๊ณตํ•  ๊ฒฝ์šฐ ์ถ”๊ฐ€ ํšŒ๋ณต)
  • ์ฒด๋ ฅ์˜ ์ตœ๋Œ€์น˜ max, ์—ฐ์†์„ฑ๊ณต ํšŸ์ˆ˜ succes ์„ค์ •
  • ๋ชฌ์Šคํ„ฐ์—๊ฒŒ ๊ณต๊ฒฉ๋‹นํ•œ ์‹œ๊ฐ„์„ attack_times์— ์ถ”๊ฐ€
  • ๋งˆ์ง€๋ง‰ ๊ณต๊ฒฉ์‹œ๊ฐ„๊นŒ์ง€ for๋ฌธ์„ ๋Œ๋ฉด์„œ ๊ณต๊ฒฉ์—ฌ๋ถ€ ํ™•์ธ
    • ํ˜„์žฌ ์‹œ๊ฐ„์ด ๊ณต๊ฒฉ๋‹นํ•œ ์‹œ๊ฐ„ attack_times์— ์žˆ์œผ๋ฉด ์ฒด๋ ฅ๊ฐ์†Œ ๋ฐ ์—ฐ์†์„ฑ๊ณต ์ดˆ๊ธฐํ™”
      • ๊ณต๊ฒฉ์„ ๋‹นํ•ด์„œ ์ฒด๋ ฅ์ด 0๋ณด๋‹ค ์ž‘์•„์ง€๋ฉด -1์„ ๋ฆฌํ„ดํ•˜๊ณ  ์ข…๋ฃŒ
    • ๊ณต๊ฒฉ๋‹นํ•˜์ง€ ์•Š์•˜์œผ๋ฉด ๋ถ•๋Œ€๊ฐ๊ธฐ๋กœ ์ฒด๋ ฅ ํšŒ๋ณต
      • ์ฒด๋ ฅ์ด max๋ณด๋‹ค ์ž‘์œผ๋ฉด ํšŒ๋ณตํ•˜๊ณ  max๋ณด๋‹ค ํฌ๋ฉด max๋กœ ์ดˆ๊ธฐํ™”
      • ์—ฐ์† ์„ฑ๊ณตํ–ˆ์„ ๊ฒฝ์šฐ ์ถ”๊ฐ€ ํšŒ๋ณต

 

 

์กฐ๊ฑด์ด ๋งŽ๊ธด ํ•œ๋ฐ ๋ณต์žกํ•˜์ง€๋Š” ์•Š์•„์„œ ํ•˜๋‚˜์”ฉ ์ƒ๊ฐํ•˜๋ฉด์„œ ํ‘ธ๋‹ˆ๊นŒ ์˜์™ธ๋กœ ๊ธˆ๋ฐฉ ํ’€๋ฆฐ ๋ฌธ์ œ์˜€๋‹ค. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค๋Š” ์žฌ๋ฏธ์žˆ๋Š” ๋ฌธ์ œ ์ƒํ™ฉ์ด ๋งŽ์•„์„œ ์ข‹๋‹ค..ใ…Žใ…Ž....

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

 

๋ฌธ์ œ

 

๋ฌธ์ œ ์„ค๋ช…

์ง€๋‚˜๋‹ค๋‹ˆ๋Š” ๊ธธ์„ 'O', ์žฅ์• ๋ฌผ์„ 'X'๋กœ ๋‚˜ํƒ€๋‚ธ ์ง์‚ฌ๊ฐํ˜• ๊ฒฉ์ž ๋ชจ์–‘์˜ ๊ณต์›์—์„œ ๋กœ๋ด‡ ๊ฐ•์•„์ง€๊ฐ€ ์‚ฐ์ฑ…์„ ํ•˜๋ คํ•ฉ๋‹ˆ๋‹ค. ์‚ฐ์ฑ…์€ ๋กœ๋ด‡ ๊ฐ•์•„์ง€์— ๋ฏธ๋ฆฌ ์ž…๋ ฅ๋œ ๋ช…๋ น์— ๋”ฐ๋ผ ์ง„ํ–‰ํ•˜๋ฉฐ, ๋ช…๋ น์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•์‹์œผ๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

["๋ฐฉํ–ฅ ๊ฑฐ๋ฆฌ", "๋ฐฉํ–ฅ ๊ฑฐ๋ฆฌ" … ]
์˜ˆ๋ฅผ ๋“ค์–ด "E 5"๋Š” ๋กœ๋ด‡ ๊ฐ•์•„์ง€๊ฐ€ ํ˜„์žฌ ์œ„์น˜์—์„œ ๋™์ชฝ์œผ๋กœ 5์นธ ์ด๋™ํ–ˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๋กœ๋ด‡ ๊ฐ•์•„์ง€๋Š” ๋ช…๋ น์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์ „์— ๋‹ค์Œ ๋‘ ๊ฐ€์ง€๋ฅผ ๋จผ์ € ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

์ฃผ์–ด์ง„ ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ•  ๋•Œ ๊ณต์›์„ ๋ฒ—์–ด๋‚˜๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
์ฃผ์–ด์ง„ ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ ์ค‘ ์žฅ์• ๋ฌผ์„ ๋งŒ๋‚˜๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
์œ„ ๋‘ ๊ฐ€์ง€์ค‘ ์–ด๋Š ํ•˜๋‚˜๋ผ๋„ ํ•ด๋‹น๋œ๋‹ค๋ฉด, ๋กœ๋ด‡ ๊ฐ•์•„์ง€๋Š” ํ•ด๋‹น ๋ช…๋ น์„ ๋ฌด์‹œํ•˜๊ณ  ๋‹ค์Œ ๋ช…๋ น์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
๊ณต์›์˜ ๊ฐ€๋กœ ๊ธธ์ด๊ฐ€ W, ์„ธ๋กœ ๊ธธ์ด๊ฐ€ H๋ผ๊ณ  ํ•  ๋•Œ, ๊ณต์›์˜ ์ขŒ์ธก ์ƒ๋‹จ์˜ ์ขŒํ‘œ๋Š” (0, 0), ์šฐ์ธก ํ•˜๋‹จ์˜ ์ขŒํ‘œ๋Š” (H - 1, W - 1) ์ž…๋‹ˆ๋‹ค.

๊ณต์›์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž์—ด ๋ฐฐ์—ด park, ๋กœ๋ด‡ ๊ฐ•์•„์ง€๊ฐ€ ์ˆ˜ํ–‰ํ•  ๋ช…๋ น์ด ๋‹ด๊ธด ๋ฌธ์ž์—ด ๋ฐฐ์—ด routes๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๋กœ๋ด‡ ๊ฐ•์•„์ง€๊ฐ€ ๋ชจ๋“  ๋ช…๋ น์„ ์ˆ˜ํ–‰ ํ›„ ๋†“์ธ ์œ„์น˜๋ฅผ [์„ธ๋กœ ๋ฐฉํ–ฅ ์ขŒํ‘œ, ๊ฐ€๋กœ ๋ฐฉํ–ฅ ์ขŒํ‘œ] ์ˆœ์œผ๋กœ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

์ œํ•œ ์‚ฌํ•ญ

  • 3 ≤ park์˜ ๊ธธ์ด ≤ 50
    • 3 ≤ park[i]์˜ ๊ธธ์ด ≤ 50
      • park[i]๋Š” ๋‹ค์Œ ๋ฌธ์ž๋“ค๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ ์‹œ์ž‘์ง€์ ์€ ํ•˜๋‚˜๋งŒ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
        • S : ์‹œ์ž‘ ์ง€์ 
        • O : ์ด๋™ ๊ฐ€๋Šฅํ•œ ํ†ต๋กœ
        • X : ์žฅ์• ๋ฌผ
    • park๋Š” ์ง์‚ฌ๊ฐํ˜• ๋ชจ์–‘์ž…๋‹ˆ๋‹ค.
  • 1 ≤ routes์˜ ๊ธธ์ด ≤ 50
    • routes์˜ ๊ฐ ์›์†Œ๋Š” ๋กœ๋ด‡ ๊ฐ•์•„์ง€๊ฐ€ ์ˆ˜ํ–‰ํ•  ๋ช…๋ น์–ด๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
    • ๋กœ๋ด‡ ๊ฐ•์•„์ง€๋Š” routes์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ๋ช…๋ น์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
    • routes์˜ ์›์†Œ๋Š” "op n"๊ณผ ๊ฐ™์€ ๊ตฌ์กฐ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, op๋Š” ์ด๋™ํ•  ๋ฐฉํ–ฅ, n์€ ์ด๋™ํ•  ์นธ์˜ ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
      • op๋Š” ๋‹ค์Œ ๋„ค ๊ฐ€์ง€์ค‘ ํ•˜๋‚˜๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
        • N : ๋ถ์ชฝ์œผ๋กœ ์ฃผ์–ด์ง„ ์นธ๋งŒํผ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.
        • S : ๋‚จ์ชฝ์œผ๋กœ ์ฃผ์–ด์ง„ ์นธ๋งŒํผ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.
        • W : ์„œ์ชฝ์œผ๋กœ ์ฃผ์–ด์ง„ ์นธ๋งŒํผ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.
        • E : ๋™์ชฝ์œผ๋กœ ์ฃผ์–ด์ง„ ์นธ๋งŒํผ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.
      • 1 ≤ n ≤ 9

 

์ž…์ถœ๋ ฅ ์˜ˆ

park routes result
["SOO","OOO","OOO"] ["E 2","S 2","W 1"] [2,1]
["SOO","OXX","OOO"] ["E 2","S 2","W 1"] [0,1]
["OSO","OOO","OXO","OOO"] ["E 2","S 3","W 1"] [0,0]

 

 

์†Œ์Šค ์ฝ”๋“œ
# 1
def solution(park, routes):
    # x = ์—ด ๋ฒˆํ˜ธ / y = ํ–‰ ๋ฒˆํ˜ธ
    # ํ–‰์—ด์˜ ์ตœ๋Œ€๊ฐ’ ์„ค์ •
    x_max = len(park[0])
    y_max = len(park)
    
    # ์‹œ์ž‘ ์œ„์น˜ ์ฐพ๊ธฐ
    x = 0
    y = 0
    for i in range(y_max):          # i = ํ–‰ ๋ฒˆํ˜ธ = y๊ฐ’
        for j in range(x_max):      # j = ์—ด ๋ฒˆํ˜ธ = x๊ฐ’
            if park[i][j] == 'S':
                x = j
                y = i
                break
    
    # ์ขŒํ‘œ ์ด๋™
    for route in routes:
        # ๋ฐฉํ–ฅ๊ณผ ์ด๋™๊ฑฐ๋ฆฌ๋ฅผ ์ €์žฅ
        direction, distance = route.split(" ")

        # ํ˜„์žฌ์˜ ์ขŒํ‘œ๋ฅผ ์ž„์‹œ๋กœ ์ด๋™
        xx = x
        yy = y

        # ํ•œ ์นธ์”ฉ ์ด๋™ํ•˜๋ฉด์„œ ๊ณต์›์„ ๋ฒ—์–ด๋‚˜๊ฑฐ๋‚˜ ์žฅ์• ๋ฌผ์„ ๋งŒ๋‚˜๋Š”์ง€ ํ™•์ธ
        # step์ด ์ด๋™๊ฑฐ๋ฆฌ์™€ ๊ฐ™์•„์ง€๋ฉด ํ˜„์žฌ ์ขŒํ‘œ๋ฅผ ์ž„์‹œ ์ขŒํ‘œ๋กœ ๋ณ€๊ฒฝ
        for step in range(1, int(distance)+1):
            # ๋™์ชฝ์œผ๋กœ ์ด๋™
            if direction == 'E' and xx != x_max-1 and park[yy][xx+1] != 'X':
                xx += 1
                if step == int(distance):
                    x = xx
            # ์„œ์ชฝ์œผ๋กœ ์ด๋™
            elif direction == 'W' and xx != 0 and park[yy][xx-1] != 'X':
                xx -= 1
                if step == int(distance):
                    x = xx
            # ๋‚จ์ชฝ์œผ๋กœ ์ด๋™
            elif direction == 'S' and yy != y_max-1 and park[yy+1][xx] != 'X':
                yy += 1
                if step == int(distance):
                    y = yy
            # ๋ถ์ชฝ์œผ๋กœ ์ด๋™
            elif direction == 'N' and yy != 0 and park[yy-1][xx] != 'X':
                yy -= 1
                if step == int(distance):
                    y = yy

    return [y, x]

 

ํ’€์ด # 1

  • ์ขŒํ‘œ์—์„œ ๊ณ ๋ ค์‚ฌํ•ญ
    • x = ์—ด ๋ฒˆํ˜ธ / y = ํ–‰ ๋ฒˆํ˜ธ
    • x_max = ์—ด์˜ ๊ธธ์ด / y_max = ํ–‰์˜ ๊ธธ์ด
  • ์‹œ์ž‘ ์œ„์น˜ ์ฐพ๊ธฐ
    • i = ํ–‰ ๋ฒˆํ˜ธ = y๊ฐ’ / j = ์—ด ๋ฒˆํ˜ธ / x๊ฐ’
    • ์œ„์˜ ๋‚ด์šฉ์„ ๊ณ ๋ คํ•˜์—ฌ ์‹œ์ž‘์  S ์ฐพ๊ธฐ
  • ์ขŒํ‘œ ์ด๋™
    • ๋„์–ด์“ฐ๊ธฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์•ž์˜ ๊ฐ’์€ ๋ฐฉํ–ฅ(direction), ๋’ค์˜ ๊ฐ’์€ ์ด๋™๊ฑฐ๋ฆฌ(distance)
    • ํ˜„์žฌ ์ขŒํ‘œ์™€ ๋™์ผํ•œ ์ž„์‹œ ์ขŒํ‘œ๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ด๋™ (์กฐ๊ฑด์„ ๋งŒ์กฑ์‹œํ‚ค์ง€ ์•Š์„ ๊ฒฝ์šฐ ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”)
      • ๋ฐฉํ–ฅ ํ™•์ธ
        • E = ๋™์ชฝ : x์ขŒํ‘œ + 1
        • W = ์„œ์ชฝ : x์ขŒํ‘œ - 1
        • S = ๋‚จ์ชฝ : y์ขŒํ‘œ + 1
        • N = ๋ถ์ชฝ : y์ขŒํ‘œ -1
      • ํ˜„์žฌ ์ขŒํ‘œ๊ฐ€ ํ–‰์ด๋‚˜ ์—ด์˜ ๊ธธ์ด๋ฅผ ๋ฒ—์–ด๋‚˜๋Š”์ง€ ํ™•์ธ
      • ํ•œ ์นธ ์ด๋™ํ–ˆ์„ ๋•Œ ์žฅ์• ๋ฌผ์ด ์žˆ๋Š”์ง€ ํ™•์ธ
    • ๊ฐ ์กฐ๊ฑด์„ ๋ชจ๋‘ ๋งŒ์กฑํ•œ ๊ฒฝ์šฐ ์ž„์‹œ ์ขŒํ‘œ๋ฅผ ํ•œ ์นธ ์ด๋™
    • step์ด ์ด๋™ ๊ฑฐ๋ฆฌ์— ๋„๋‹ฌํ•˜๋ฉด ํ˜„์žฌ ์ขŒํ‘œ๋ฅผ ์ž„์‹œ ์ขŒํ‘œ๋กœ ๋ฐ”๊พธ๊ธฐ
    • x๋Š” ์—ด ๋ฒˆํ˜ธ, y๋Š” ํ–‰ ๋ฒˆํ˜ธ์ด๋ฏ€๋กœ [y,x] ๋ฅผ ๋ฐ˜ํ™˜

 

# 2
def solution(park, routes):
    # ํ–‰์—ด์˜ ์ตœ๋Œ€๊ฐ’ ์„ค์ •
    w, h = len(park[0]), len(park)

    # ๋ฐฉํ–ฅ๋ณ„๋กœ ์ด๋™ํ•ด์•ผํ•˜๋Š” ๊ฑฐ๋ฆฌ๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ์— ์ €์žฅ
    move = {"N":(-1,0), "S":(1,0), "W":(0,-1), "E":(0,1)}
    
    # ์‹œ์ž‘ ์œ„์น˜ ์ฐพ๊ธฐ
    x, y = 0, 0
    for i in range(h):
        for j in range(w):
            if park[i][j] == "S":
                x, y = i, j
                break
        
    # ์ขŒํ‘œ ์ด๋™
    for route in routes:
        # ๋ฐฉํ–ฅ๊ณผ ์ด๋™๊ฑฐ๋ฆฌ๋ฅผ ์ €์žฅ
        direction, distance = route.split(" ")

        # ํ˜„์žฌ์˜ ์ขŒํ‘œ๋ฅผ ์ €์žฅํ•ด์„œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜์ง€ ๋ชปํ•˜๋ฉด ํ•ด๋‹น๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”
        dx, dy = x, y
        
        for idx in range(int(distance)):
            # ์ด๋™ํ•  ์œ„์น˜
            nx = x + move[direction][0]
            ny = y + move[direction][1]
        
            if 0 <= nx <= h-1 and 0 <= ny <= w-1 and (park[nx][ny]!="X"):
                x, y = nx, ny                    
            else:
                x, y = dx, dy
                break

    return [x,y]

 

ํ’€์ด #2

  • ์œ„์˜ ํ’€์ด์™€ ๋ฐ˜๋Œ€๋กœ x๋ฅผ ํ–‰ ๋ฒˆํ˜ธ, y๋ฅผ ํ–‰ ๋ฒˆํ˜ธ๋กœ ์ƒ๊ฐํ•œ ํ’€์ด
  • ํ‚ค๊ฐ€ ์ด๋™ ๋ฐฉํ–ฅ, ๊ฐ’์ด ์ด๋™ํ•ด์•ผํ•˜๋Š” ๊ฑฐ๋ฆฌ์ธ ๋”•์…”๋„ˆ๋ฆฌ ์ƒ์„ฑ
    • E = ๋™์ชฝ : y์ขŒํ‘œ + 1
    • W = ์„œ์ชฝ : y์ขŒํ‘œ - 1
    • S = ๋‚จ์ชฝ : x์ขŒํ‘œ + 1
    • N = ๋ถ์ชฝ : x์ขŒํ‘œ -1
  • ์‹œ์ž‘ ์œ„์น˜ ์ฐพ๊ธฐ
  • ํ˜„์žฌ ์ขŒํ‘œ๋ฅผ dx, dy์— ์ €์žฅ (์กฐ๊ฑด์„ ๋งŒ์กฑ์‹œํ‚ค์ง€ ์•Š์„ ๊ฒฝ์šฐ ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”)
  • ์ž„์‹œ ์ขŒํ‘œ nx, ny๋ฅผ ๋งŒ๋“ค์–ด ๋ชจ๋“  ์กฐ๊ฑด์„ ๋งŒ์กฑํ–ˆ์„ ๊ฒฝ์šฐ์— ํ˜„์žฌ ์ขŒํ‘œ๋ฅผ ๋ณ€๊ฒฝ

 

 

x์ขŒํ‘œ, y์ขŒํ‘œ ๋‚˜์˜ค๋ฉด ์ผ๋‹จ ํ—ท๊ฐˆ๋ฆฌ๊ธฐ ์‹œ์ž‘....๋‹ค์‹œ ์ฐจ๊ทผ์ฐจ๊ทผ ์ƒ๊ฐํ•ด๋ณด๊ฒ ๋‹ค๋ฉฐ ์ฝ”๋“œ 12938091๋ฒˆ ์ˆ˜์ •ํ–ˆ๋Š”๋ฐ ๊ฐœ์ธ์ ์œผ๋กœ ์ฒซ ๋ฒˆ์งธ ํ’€์ด๊ฐ€ ์ข€ ๋” ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์› ๋‹ค. ๋‘ ๋ฒˆ์งธ ํ’€์ด๋Š” ์†๋„๊ฐ€ ์กฐ๊ธˆ ๋” ๋น ๋ฅด์ง€๋งŒ ๋จธ๋ฆฌ๊ฐ€ ๋”ฐ๋ผ์ฃผ์ง€ ์•Š์•„์„œ ํž˜๋“ค์—ˆ๋‹ค...ํœด...์–ด์จŒ๋“  ํ’€์—ˆ์œผ๋‹ˆ๊นŒ ์‰ฌ์–ด์•ผ๊ฒ ๋‹ค.๊ณต์› ์‚ฐ์ฑ…์„ ์™œ์ด๋ ‡๊ฒŒ ๋นก์„ธ๊ฒŒ ํ•˜์‹œ๋Š” ๊ฑฐ์—์š”... ๐Ÿƒ๐Ÿป ๐Ÿƒ๐Ÿป

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

 

๋ฌธ์ œ

 

๋ฌธ์ œ ์„ค๋ช…

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

๊ฐ ๊ธฐ์‚ฌ๋Š” ์ž์‹ ์˜ ๊ธฐ์‚ฌ ๋ฒˆํ˜ธ์˜ ์•ฝ์ˆ˜ ๊ฐœ์ˆ˜์— ํ•ด๋‹นํ•˜๋Š” ๊ณต๊ฒฉ๋ ฅ์„ ๊ฐ€์ง„ ๋ฌด๊ธฐ๋ฅผ ๊ตฌ๋งคํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, ์ด์›ƒ๋‚˜๋ผ์™€์˜ ํ˜‘์•ฝ์— ์˜ํ•ด ๊ณต๊ฒฉ๋ ฅ์˜ ์ œํ•œ์ˆ˜์น˜๋ฅผ ์ •ํ•˜๊ณ , ์ œํ•œ์ˆ˜์น˜๋ณด๋‹ค ํฐ ๊ณต๊ฒฉ๋ ฅ์„ ๊ฐ€์ง„ ๋ฌด๊ธฐ๋ฅผ ๊ตฌ๋งคํ•ด์•ผ ํ•˜๋Š” ๊ธฐ์‚ฌ๋Š” ํ˜‘์•ฝ๊ธฐ๊ด€์—์„œ ์ •ํ•œ ๊ณต๊ฒฉ๋ ฅ์„ ๊ฐ€์ง€๋Š” ๋ฌด๊ธฐ๋ฅผ ๊ตฌ๋งคํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, 15๋ฒˆ์œผ๋กœ ์ง€์ •๋œ ๊ธฐ์‚ฌ๋‹จ์›์€ 15์˜ ์•ฝ์ˆ˜๊ฐ€ 1, 3, 5, 15๋กœ 4๊ฐœ ์ด๋ฏ€๋กœ, ๊ณต๊ฒฉ๋ ฅ์ด 4์ธ ๋ฌด๊ธฐ๋ฅผ ๊ตฌ๋งคํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ์ด์›ƒ๋‚˜๋ผ์™€์˜ ํ˜‘์•ฝ์œผ๋กœ ์ •ํ•ด์ง„ ๊ณต๊ฒฉ๋ ฅ์˜ ์ œํ•œ์ˆ˜์น˜๊ฐ€ 3์ด๊ณ  ์ œํ•œ์ˆ˜์น˜๋ฅผ ์ดˆ๊ณผํ•œ ๊ธฐ์‚ฌ๊ฐ€ ์‚ฌ์šฉํ•  ๋ฌด๊ธฐ์˜ ๊ณต๊ฒฉ๋ ฅ์ด 2๋ผ๋ฉด, 15๋ฒˆ์œผ๋กœ ์ง€์ •๋œ ๊ธฐ์‚ฌ๋‹จ์›์€ ๋ฌด๊ธฐ์ ์—์„œ ๊ณต๊ฒฉ๋ ฅ์ด 2์ธ ๋ฌด๊ธฐ๋ฅผ ๊ตฌ๋งคํ•ฉ๋‹ˆ๋‹ค. ๋ฌด๊ธฐ๋ฅผ ๋งŒ๋“ค ๋•Œ, ๋ฌด๊ธฐ์˜ ๊ณต๊ฒฉ๋ ฅ 1๋‹น 1kg์˜ ์ฒ ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋ฌด๊ธฐ์ ์—์„œ ๋ฌด๊ธฐ๋ฅผ ๋ชจ๋‘ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์ฒ ์˜ ๋ฌด๊ฒŒ๋ฅผ ๋ฏธ๋ฆฌ ๊ณ„์‚ฐํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค.

๊ธฐ์‚ฌ๋‹จ์›์˜ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ number์™€ ์ด์›ƒ๋‚˜๋ผ์™€ ํ˜‘์•ฝ์œผ๋กœ ์ •ํ•ด์ง„ ๊ณต๊ฒฉ๋ ฅ์˜ ์ œํ•œ์ˆ˜์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ limit์™€ ์ œํ•œ์ˆ˜์น˜๋ฅผ ์ดˆ๊ณผํ•œ ๊ธฐ์‚ฌ๊ฐ€ ์‚ฌ์šฉํ•  ๋ฌด๊ธฐ์˜ ๊ณต๊ฒฉ๋ ฅ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ power๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋ฌด๊ธฐ์ ์˜ ์ฃผ์ธ์ด ๋ฌด๊ธฐ๋ฅผ ๋ชจ๋‘ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์ฒ ์˜ ๋ฌด๊ฒŒ๋ฅผ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜์‹œ์˜ค.

 

์ œํ•œ ์‚ฌํ•ญ

  • 1 ≤ number ≤ 100,000
  • 2 ≤ limit ≤ 100
  • 1 ≤ power ≤ limit

 

์ž…์ถœ๋ ฅ ์˜ˆ

number limit power result
5 3 2 10
10 3 2 21

 

 

์†Œ์Šค ์ฝ”๋“œ
# 1 (์‹œ๊ฐ„ ์ดˆ๊ณผ)
def solution(number,limit,power):
    answer = 0
    knights = list(range(1,number+1))
    
    for knight in knights:
        cnt = 0
        for n in range(1,knight+1):
            if knight%n == 0:
                cnt += 1
        if cnt > limit:
            answer += power
        else:
            answer += cnt

    return answer

 

ํ’€์ด # 1

  • knights ๋ฆฌ์ŠคํŠธ์— ๊ฐ ๊ธฐ์‚ฌ๋“ค์˜ ๋ฒˆํ˜ธ ๋‹ด๊ธฐ
  • knights ๋ฆฌ์ŠคํŠธ๋ฅผ ๋Œ๋ฉด์„œ ๊ฐ ๊ธฐ์‚ฌ ๋ฒˆํ˜ธ์˜ ์•ฝ์ˆ˜ ๊ฐฏ์ˆ˜๋ฅผ cnt ๋ณ€์ˆ˜์— ๋„ฃ๊ธฐ
    • cnt๊ฐ€ limit๋ณด๋‹ค ํฌ๋‹ค๋ฉด ์ •ํ•ด์ง„ power๋ฅผ answer์— ๋”ํ•˜๊ธฐ
    • cnt๊ฐ€ limit๋ณด๋‹ค ์ž‘๋‹ค๋ฉด cnt๋ฅผ answer์— ๋”ํ•˜๊ธฐ

 

# 2
def solution(number,limit,power):
    knights = list(range(1,number+1))
    knights_score = []
    
    for knight in knights:
        cnt = 0
        for n in range(1,int(knight**(1/2))+1):
            if knight%n == 0:
                cnt += 1
                if n**2 != knight: 
                    cnt += 1
            if cnt > limit:
                cnt = power
                break
        knights_score.append(cnt)

    return sum(knights_score)

 

ํ’€์ด #2

  • knights ๋ฆฌ์ŠคํŠธ์— ๊ฐ ๊ธฐ์‚ฌ๋“ค์˜ ๋ฒˆํ˜ธ ๋‹ด๊ธฐ
  • knights ๋ฆฌ์ŠคํŠธ๋ฅผ ๋Œ๋ฉด์„œ ๊ฐ ๊ธฐ์‚ฌ ๋ฒˆํ˜ธ์˜ ์•ฝ์ˆ˜ ๊ฐฏ์ˆ˜๋ฅผ cnt ๋ณ€์ˆ˜์— ๋„ฃ๊ธฐ
    • ์ด ๋•Œ, ๊ธฐ์‚ฌ ๋ฒˆํ˜ธ์˜ ์ œ๊ณฑ๊ทผ๊นŒ์ง€์˜ ๋ฒ”์œ„ ์‚ฌ์šฉ (์•ฝ์ˆ˜๋Š” ์ง์ด ์žˆ์œผ๋ฏ€๋กœ ์ œ๊ณฑ๊ทผ๊นŒ์ง€๋งŒ ๋ฐ˜๋ณตํ•˜์—ฌ ์ดํ›„์— ์ œ๊ณฑ)
    • ์•ฝ์ˆ˜์˜ ๊ฐฏ์ˆ˜๊ฐ€ limit์„ ๋„˜์–ด๊ฐ€๋ฉด for๋ฌธ์„ ์ข…๋ฃŒํ•˜์—ฌ ์—ฐ์‚ฐ์˜ ์ˆ˜ ์ค„์ด๊ธฐ
  • knights_score ๋ฆฌ์ŠคํŠธ์— ๊ฐ ๊ธฐ์‚ฌ์˜ ์ ์ˆ˜๋ฅผ ์ž…๋ ฅ
  • knights_score์˜ ํ•ฉ์„ ๋ฆฌํ„ด

 

 

์‹œ๊ฐ„ ๋ณต์žก๋„ ๋ฌธ์ œ๊ฐ€ ๋‚˜์™”๋Š”๋ฐ์š”...? ์ œ์ถœํ•˜๊ณ  ์‹œ๊ฐ„ ์ดˆ๊ณผ ๋œจ๋‹ˆ๊นŒ ์•„์ฐ”ํ•ด์„œ ์ธ์ƒ์˜ ์žฌ๋ฏธ๋ฅผ ๋Š๋‚„ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ํ’€์ด๋กœ ํ–ˆ์„ ๋•Œ ํ…Œ์ŠคํŠธ1์— 3000ms ๊ฑธ๋ ธ๋Š”๋ฐ ๋‘ ๋ฒˆ์งธ ํ’€์ด๋กœ ํ•˜๋‹ˆ๊นŒ 50ms๊ฐ€ ๋‚˜์™”๋‹ค. ๋‘ ๋ฒˆ์งธ ํ’€์ด๋Š” ๋ธ”๋กœ๊ทธ ๊ธ€์„ ์ฐธ๊ณ ํ–ˆ๋Š”๋ฐ ์„ค๋ช…์„ ์ž˜ํ•ด์ฃผ์…”์„œ ์ดํ•ด๊ฐ€ ์ž˜ ๋๋‹ค.

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

 

๋ฌธ์ œ

 

๋ฌธ์ œ ์„ค๋ช…

๋‘ ์ •์ˆ˜ X, Y์˜ ์ž„์˜์˜ ์ž๋ฆฌ์—์„œ ๊ณตํ†ต์œผ๋กœ ๋‚˜ํƒ€๋‚˜๋Š” ์ •์ˆ˜ k(0 ≤ k ≤ 9)๋“ค์„ ์ด์šฉํ•˜์—ฌ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ์ •์ˆ˜๋ฅผ ๋‘ ์ˆ˜์˜ ์ง๊ฟ์ด๋ผ ํ•ฉ๋‹ˆ๋‹ค(๋‹จ, ๊ณตํ†ต์œผ๋กœ ๋‚˜ํƒ€๋‚˜๋Š” ์ •์ˆ˜ ์ค‘ ์„œ๋กœ ์ง์ง€์„ ์ˆ˜ ์žˆ๋Š” ์ˆซ์ž๋งŒ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค). X, Y์˜ ์ง๊ฟ์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด, ์ง๊ฟ์€ -1์ž…๋‹ˆ๋‹ค. X, Y์˜ ์ง๊ฟ์ด 0์œผ๋กœ๋งŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค๋ฉด, ์ง๊ฟ์€ 0์ž…๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, X = 3403์ด๊ณ  Y = 13203์ด๋ผ๋ฉด, X์™€ Y์˜ ์ง๊ฟ์€ X์™€ Y์—์„œ ๊ณตํ†ต์œผ๋กœ ๋‚˜ํƒ€๋‚˜๋Š” 3, 0, 3์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ์ •์ˆ˜์ธ 330์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์˜ˆ์‹œ๋กœ X = 5525์ด๊ณ  Y = 1255์ด๋ฉด X์™€ Y์˜ ์ง๊ฟ์€ X์™€ Y์—์„œ ๊ณตํ†ต์œผ๋กœ ๋‚˜ํƒ€๋‚˜๋Š” 2, 5, 5๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ์ •์ˆ˜์ธ 552์ž…๋‹ˆ๋‹ค(X์—๋Š” 5๊ฐ€ 3๊ฐœ, Y์—๋Š” 5๊ฐ€ 2๊ฐœ ๋‚˜ํƒ€๋‚˜๋ฏ€๋กœ ๋‚จ๋Š” 5 ํ•œ ๊ฐœ๋Š” ์ง ์ง€์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.)
๋‘ ์ •์ˆ˜ X, Y๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, X, Y์˜ ์ง๊ฟ์„ returnํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

์ œํ•œ ์‚ฌํ•ญ

  • 3 ≤ X, Y์˜ ๊ธธ์ด(์ž๋ฆฟ์ˆ˜) ≤ 3,000,000์ž…๋‹ˆ๋‹ค.
  • X, Y๋Š” 0์œผ๋กœ ์‹œ์ž‘ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • X, Y์˜ ์ง๊ฟ์€ ์ƒ๋‹นํžˆ ํฐ ์ •์ˆ˜์ผ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ

X Y result
"100" "2345" "-1"
"100" "203045" "0"
"100" "123450" "10"
"12321" "42531" "321"
"5525" "1255" "552"

 

 

์†Œ์Šค ์ฝ”๋“œ
# 1 (์‹œ๊ฐ„ ์ดˆ๊ณผ)
def solution(X, Y):
    answer = ""

    inter = []

    for x in X:
        if x in Y:
            inter.append(int(x))
            Y = Y.replace(x,"",1)  

    inter.sort(reverse=True) 
    if len(inter) == 0:
        answer = "-1"
    elif inter[0] == 0:
        answer = "0"
    else:
        for i in inter:
            answer += str(i)
        
    return answer

 

ํ’€์ด # 1

  • ๊ต์ง‘ํ•ฉ ์›์†Œ๋ฅผ ๋‹ด์„ inter ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ
  • X๋ฅผ ๋Œ๋ฉด์„œ X์˜ ์›์†Œ๊ฐ€ Y์— ์žˆ์œผ๋ฉด inter ๋ฆฌ์ŠคํŠธ์— ํ•ด๋‹น ์›์†Œ ์ถ”๊ฐ€ํ•˜๊ณ  Y์—์„œ ํ•ด๋‹น ์›์†Œ ์‚ญ์ œ
    • replace("๋ฐ”๊พธ๊ธฐ ์ „ ๋ฌธ์ž", "๋ฐ”๊พผ ํ›„์˜ ๋ฌธ์ž", ๋ฐ”๊พธ๊ณ  ์‹ถ์€ ๋ฌธ์ž ๊ฐฏ์ˆ˜)
  • inter ๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ
  • inter๊ฐ€ ์—†์„ ๊ฒฝ์šฐ "-1", 0์ด ์—ฌ๋Ÿฌ๊ฐœ์ธ ๊ฒฝ์šฐ์—๋Š” "0"์„ ์ถœ๋ ฅ
  • inter๊ฐ€ ์žˆ๊ณ  0์ด ์•„๋‹ˆ๋ผ๋ฉด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋Œ๋ฉด์„œ ๋ฌธ์ž๋ฅผ answer์— ์ถ”๊ฐ€

 

# 2
def solution(X, Y):
    answer = ""

    numberX = [0]*10
    numberY = [0]*10
    
    # ๊ฐ ์ˆซ์ž๊ฐ€ ๋ช‡ ๊ฐœ์”ฉ ์žˆ๋Š”์ง€ ์นด์šดํŠธ
    for x in X:
        numberX[int(x)] += 1
    
    for y in Y:
        numberY[int(y)] += 1


    # ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฉด์„œ ์ถ”๊ฐ€
    for idx in range(9,-1,-1):
        if numberX[idx] > 0 and numberY[idx] > 0:
            cnt = min(numberX[idx], numberY[idx])
            answer += (str(idx) * cnt)

    if answer == "":
        answer = "-1"
    elif answer[0] == "0":
        answer = "0"

        
    return answer

 

ํ’€์ด #2

  • X์™€ Y์˜ ๊ฐ ์ˆซ์ž ๊ฐฏ์ˆ˜๋ฅผ numberX์™€ numberY์— ์นด์šดํŠธ (๊ฐ ์ˆซ์ž = ์ธ๋ฑ์Šค)
  • sort๋ฅผ ํ•˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด ๊ฐ€์žฅ ํฐ ์ˆ˜๋ถ€ํ„ฐ ๋ฐ˜๋ณต๋ฌธ
    • numberX์™€ numberY์— ๋‘˜ ๋‹ค ์กด์žฌํ•˜๋ฉด ๋‘˜ ์ค‘์— ์ž‘์€ ์ˆ˜๋งŒํผ answer์— ์ถ”๊ฐ€
  • ๊ต์ง‘ํ•ฉ์ด ์—†๊ฑฐ๋‚˜ 0์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ

 

 

๋ฌธ์ œ ์ž์ฒด๋Š” ๋‹จ์ˆœํ•œ๋ฐ ์ œํ•œ ์‚ฌํ•ญ์„ ๋ณด๋ฉด ์ •์ˆ˜๊ฐ€ ์ƒ๋‹นํžˆ ํด ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ์กฐ๊ฑด์ด ์žˆ๋‹ค. sort๋ฅผ ์ด์šฉํ•˜๋ฉด ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋ฏ€๋กœ sort๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  for๋ฌธ๋„ 10๊ฐœ ์ˆซ์ž ๋‚ด์—์„œ ๋Œ๋„๋ก ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜์˜€๋‹ค. ํ†ต๊ณผ๋Š” ํ–ˆ์ง€๋งŒ ์‹œ๊ฐ„์ด ์ ๊ฒŒ ๊ฑธ๋ฆฌ๋Š” ๊ฒƒ ๊ฐ™์ง€๋Š” ์•Š๋‹ค. ์‹œ๊ฐ„ ๋ณต์žก๋„๋„ ๊ณ ๋ คํ•ด์„œ ์ฝ”๋“œ๋ฅผ ์งœ๋Š” ๊ฒŒ ์ค‘์š”ํ•œ๋ฐ....์–ด๋ ต๊ตฌ๋งŒ...

+ Recent posts