๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค LV2 ๊ณผ์ œ ์ง„ํ–‰ํ•˜๊ธฐ

๐ŸŽฎinspirer9 2023. 8. 4. 23:09
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์ž์ฒด๋Š” ์–ด๋ ต์ง€ ์•Š์•˜๋‹ค.

  • ์•ž์„  ์ž‘์—…์ด ๋๋‚˜์ง€ ์•Š์€ ์ƒํƒœ์—์„œ ์ƒˆ๋กœ์šด ์ž‘์—…์ด ์ถ”๊ฐ€๋˜๋ฉด ๋ฉˆ์ถ˜๋‹ค.
  • ๋ณด์ž๋งˆ์ž "์Šคํƒ"์ด์—ˆ๋Š”๋ฐ...

๊ทผ๋ฐ ์ด ๋ฌธ์ œ๋Š” ์‚ฝ์งˆ์„ ๋„ˆ๋ฌด ์˜ค๋ž˜ ํ•ด์„œ ๋‚ด๊ฐ€ ์ง  ์ฝ”๋“œ๋„ ์ดํ•ด๊ฐ€ ์•ˆ๋˜๊ณ  ์™œ ์ €๋ ‡๊ฒŒ ํ–ˆ๋Š”์ง€ ๊ธฐ์–ต์ด ๋‚˜์งˆ ์•Š์•˜๋‹ค. ๊ทธ๋ž˜์„œ ์˜ˆ์ „์— ์ง  ์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ๋ถ„์„ํ•ด๋ณด์•˜๋‹ค.

  • ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ถ€๋ถ„์—์„œ ๋ฏธ๋ฆฌ ๋ญ”๊ฐ€๋ฅผ ๊ณ„์‚ฐํ•˜๋‹ค๋ณด๋‹ˆ ๋น„๊ตํ•˜๋Š” ๋ถ€๋ถ„์ด ์—‰ํ„ฐ๋ฆฌ์˜€๋‹ค.
    • ์ด์ „์— ์ €์žฅํ•œ ํ˜„์žฌ ์‹œ๊ฐ„๋ณด๋‹ค ์ž‘์—… ์‹œ์ž‘ ์‹œ๊ฐ„์ด ๋‚˜์ค‘์ด๋ฉด ํ˜„์žฌ ์‹œ๊ฐ„ ๊ฐฑ์‹ 
    • ๋‹ค์Œ ์ž‘์—… ์‹œ์ž‘ ์‹œ๊ฐ„๋ณด๋‹ค ํ˜„์žฌ ์‹œ๊ฐ„ + ์ž‘์—… ์‹œ๊ฐ„์ด ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด ํ˜„์žฌ ์‹œ๊ฐ„์— ์ž‘์—… ์‹œ๊ฐ„ ๋”ํ•˜๊ณ  answer์— ๋„ฃ์Œ
    • ๋‹ค์Œ ์ž‘์—… ์‹œ์ž‘ ์‹œ๊ฐ„๋ณด๋‹ค ํ˜„์žฌ ์‹œ๊ฐ„ + ์ž‘์—… ์‹œ๊ฐ„์ด ํฌ๋ฉด ํ˜„์žฌ ์‹œ๊ฐ„์„ ๋‹ค์Œ ์ž‘์—… ์‹œ์ž‘ ์‹œ๊ฐ„์œผ๋กœ ๊ฐฑ์‹ ํ•˜๊ณ  ์ž‘์—… ํ์— ๋‚จ์€ ์ž‘์—… ์‹œ๊ฐ„(ํ˜„์žฌ ์‹œ๊ฐ„ + ์ž‘์—… ์‹œ๊ฐ„ - ๋‹ค์Œ ์ž‘์—… ์‹œ๊ฐ„)์„ ๋„ฃ์–ด์คŒ
    • ๋‹ค์Œ ์ž‘์—…์„ ์ž‘์—… ํ์— ๋„ฃ์–ด์คŒ
    • ๋ชจ๋“  ํ”Œ๋žœ์„ ๋Œ๋ฉด ์ž‘์—… ํ์— ๋‚จ์€ ์ž‘์—…๋“ค ์ˆœ์ฐจ์ ์œผ๋กœ ์ž‘์—…ํ•˜๊ณ  answer์— ๋„ฃ์Œ.

์ €๋ ‡๊ฒŒ ํ•  ํ•„์š”๊ฐ€ ์—†์ด ๊ทธ๋ƒฅ ํ•œ ์ค„๋กœ ์„ธ์›Œ์„œ ์ •๋ฆฌํ•˜๊ณ  ๋‹ค์Œ ์ž‘์—…๊ณผ ์Šคํƒ์„ ๋น„๊ตํ•˜๊ธฐ๋ฅผ ๋ฐ˜๋ณต...

  • ์ž‘์—… ํ์—์„œ ์ฒซ ์ž‘์—…์„ ๋นผ์„œ ์Šคํƒ์— ๋„ฃ์Œ
  • (๋ฐ˜๋ณต) ์ž‘์—… ํ์—์„œ ๋‹ค์Œ ์ž‘์—…์„ ๋นผ์„œ...
    • ์Šคํƒ์— ์ž‘์—…์ด ์žˆ๋Š”์ง€ ํ™•์ธ
      • ์Šคํƒ์—์„œ ๋นผ์„œ ๋น„๊ตํ•˜๋Š” ์ž‘์—… ์‹œ์ž‘
    • ์Šคํƒ์— ์ž‘์—…์ด ์—†์œผ๋ฉด 
      • ์Šคํƒ์— ์ž‘์—… ๋„ฃ๊ธฐ
def solution(plans):
    answer = []
    
    for i,v in enumerate(plans): # ์‹œ๊ฐ„ ๋ฌธ์ž์—ด์„ ์ˆซ์ž๋กœ ๋ณ€๊ฒฝ
        job, t1, t2 = v
        hh,mm = t1.split(":")
        plans[i][1],plans[i][2] = int(hh) * 60 + int(mm), int(t2)

    plans.sort(key=lambda x:x[1]) # ์ •๋ ฌ
        
    stack = [] # ์Šคํƒ ์ดˆ๊ธฐํ™”
    stack.append(plans[0]) # ์Šคํƒ์— ์ž‘์—… ๋„ฃ๊ธฐ
    
    time = plans[0][1] # ์ฒซ ์ž‘์—… ์‹œ์ž‘ ์‹œ๊ฐ„
    
    for i in range(1, len(plans)):
        next_time = plans[i][1] # ๋‹ค์Œ ์ž‘์—… ์‹œ์ž‘ ์‹œ๊ฐ„

        while len(stack): # ์Šคํƒ์— ์ž‘์—…์ด ์žˆ์œผ๋ฉด
            job, time_start, time_spend = stack.pop() # ์Šคํƒ์—์„œ ์ž‘์—… ๊บผ๋‚ด๊ธฐ
            
            if time < time_start: # ํ˜„์žฌ ์‹œ๊ฐ„๋ณด๋‹ค ์Šคํƒ์— ์žˆ๋Š” ์ž‘์—… ์‹œ์ž‘ ์‹œ๊ฐ„์ด ๋Š๋ฆฌ๋ฉด
                time = time_start # ์‹œ๊ฐ„ ์กฐ์ •
                
            time_finish = time + time_spend # ์Šคํƒ์˜ ์ž‘์—…์ด ๋๋‚˜๋Š” ์‹œ๊ฐ„

            if next_time < time_finish: # ๋‹ค์Œ ์ž‘์—… ์‹œ์ž‘ ์‹œ๊ฐ„์ด ์Šคํƒ์— ์žˆ๋Š” ์ž‘์—… ์™„๋ฃŒ์‹œ๊ฐ„๋ณด๋‹ค ๋น ๋ฅด๋ฉด 
                stack.append([job, time_start, time_finish - next_time]) # ์Šคํƒ์—์„œ ๊บผ๋‚ธ ์ž‘์—…์„ ๋‹ค์‹œ ๋„ฃ๊ณ 
                time = next_time # ํ˜„์žฌ ์‹œ๊ฐ„์€ ๋‹ค์Œ ์ž‘์—… ์‹œ์ž‘ ์‹œ๊ฐ„์œผ๋กœ ๋ณ€๊ฒฝ
                break # ์ค‘์ง€
            else:
                answer.append(job) # ์Šคํƒ์˜ ์ž‘์—…์„ ์™„๋ฃŒํ•œ๋‹ค.
                time += time_spend # ํ˜„์žฌ ์‹œ๊ฐ„์— ์†Œ์š” ์‹œ๊ฐ„์„ ๋”ํ•œ๋‹ค.
                                   # ์Šคํƒ์— ์ž‘์—…์ด ์žˆ์œผ๋ฉด ๊ณ„์†

        stack.append(plans[i]) # ์Šคํƒ์— ์ž‘์—… ๋„ฃ๊ธฐ

    while len(stack): # ์Šคํƒ์— ๋‚จ์€ ์ž‘์—… ๊บผ๋‚ด์„œ ์™„๋ฃŒํ•˜๊ธฐ
        answer.append(stack.pop()[0])

    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.4MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.4MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.3MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.07ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.3MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.12ms, 10.4MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.15ms, 10.4MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.18ms, 10.3MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.29ms, 10.6MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.25ms, 10.4MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.42ms, 10.3MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.75ms, 10.4MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (1.08ms, 10.4MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (1.47ms, 10.6MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (2.56ms, 10.8MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.4MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.28ms, 10.3MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰	ํ†ต๊ณผ (0.27ms, 10.4MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰	ํ†ต๊ณผ (1.40ms, 10.6MB)
ํ…Œ์ŠคํŠธ 23 ใ€‰	ํ†ต๊ณผ (2.76ms, 10.8MB)
ํ…Œ์ŠคํŠธ 24 ใ€‰	ํ†ต๊ณผ (1.77ms, 10.8MB)

๊ณ ์ˆ˜์˜ ํ’€์ด... ๋ญ”๊ฐ€ ๋˜๊ฒŒ ๊ฐ„๋‹จํ•ด์ ธ ๋ฒ„๋ ธ๋‹ค.

def solution(plans):
    plans = sorted(map(lambda x: [x[0], int(x[1][:2]) * 60 + int(x[1][3:]), int(x[2])], plans), key=lambda x: -x[1])

    lst = []
    while plans:
        x = plans.pop()
        for i, v in enumerate(lst):
            if v[0] > x[1]:
                lst[i][0] += x[2]
        lst.append([x[1] + x[2], x[0]])
    lst.sort()

    return list(map(lambda x: x[1], lst))
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.3MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.07ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.08ms, 10.3MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.11ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.52ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.33ms, 10.4MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (1.95ms, 10.4MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (1.19ms, 10.4MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (3.35ms, 10.3MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (28.39ms, 10.7MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (20.49ms, 10.7MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (84.96ms, 10.7MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (89.17ms, 10.6MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.5MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.3MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.3MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.86ms, 10.2MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰	ํ†ต๊ณผ (0.93ms, 10.3MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰	ํ†ต๊ณผ (85.63ms, 10.6MB)
ํ…Œ์ŠคํŠธ 23 ใ€‰	ํ†ต๊ณผ (87.52ms, 10.6MB)
ํ…Œ์ŠคํŠธ 24 ใ€‰	ํ†ต๊ณผ (81.80ms, 10.6MB)

๋‚œ ์•„์ง๋„ ๊ฐˆ ๊ธธ์ด ๋ฉ€๊ตฌ๋‚˜...

728x90
๋ฐ˜์‘ํ˜•