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๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์–ด์„œ ํŽธํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค.
 

+ Recent posts