๊ธฐ๋ก๋ฐฉ

Lv.2 : ์ฃผ์ฐจ ์š”๊ธˆ ๊ณ„์‚ฐ ๋ณธ๋ฌธ

CodingTest/Python

Lv.2 : ์ฃผ์ฐจ ์š”๊ธˆ ๊ณ„์‚ฐ

Soom_1n 2022. 9. 19. 18:06

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr


 


๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ์ฐจ๋Ÿ‰๋งˆ๋‹ค ์ฃผ์ฐจ์žฅ ์ด์šฉ์‹œ๊ฐ„์„ ๋ˆ„์  ํ•ฉ์‚ฐํ•œ๋‹ค.
  • ์š”๊ธˆ์„ ๊ณ„์‚ฐํ•ด ์ฐจ๋Ÿ‰๋ฒˆํ˜ธ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import math

def solution(fees, records):
    parking = {}
    check = {}
    answer = {}

    for i in records:
        time, number, order = i.split()
        now = list(map(int,time.split(':')))
        now_m = now[0] * 60 + now[1]
        if order == "IN":
            parking[number] = now_m
            check[number] = True
            if number not in answer:
                answer[number] = 0
        else:
            check[number] = False
            answer[number] += now_m - parking[number]

    for i, flag in check.items():
        if flag:
            answer[i] += (23*60+59) - parking[i]
            check[i] = True

    for num, v in answer.items():
        if v > fees[0]:  # ์š”๊ธˆ ๊ณ„์‚ฐ
            answer[num] = fees[1] + (math.ceil((v - fees[0])/fees[2]))*fees[3]
        else:
            answer[num] = fees[1]
    
    return list(dict(sorted(answer.items())).values())

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ์ฃผ์ฐจ ์ด์šฉ ๋‚ด์—ญ๋Œ€๋กœ ์‹œ๊ฐ„์„ ๋ˆ„์ ํ•˜๊ณ , ๋งˆ์ง€๋ง‰ ์ถœ์ฐจ ๊ธฐ๋ก์ด ์—†๋Š” ๊ฒฝ์šฐ๋Š” ๋”ฐ๋กœ ํ•ฉํ•œ๋‹ค.
  • ์š”๊ธˆ์„ ๊ณ„์‚ฐํ•œ๋‹ค.
    • math.ceil()์„ ์ด์šฉํ•ด ์˜ฌ๋ฆผ์„ ๊ตฌํ˜„ํ–ˆ๋‹ค.
  • ๋”•์…”๋„ˆ๋ฆฌ key๊ฐ’์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ ํ›„ values๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

๐Ÿ”ธ ๋‹ค๋ฅธ ํ’€์ด ๐Ÿ”ธ

from collections import defaultdict

def solution(fees, records):
    
    basic_t, base_price, unit_t, unit_price = fees
    
    def price(log, fees): # 2 : ์ฐจ ๋ฒˆํ˜ธ๋ณ„๋กœ ๊ฐ€๊ฒฉ ์ธก์ •(1๋ฒˆ ๊ฒฐ๊ณผ ์‚ฌ์šฉ)
        if len(log) % 2:
            log.append(23*60+59)
        t = sum(log[i+1] - log[i] for i in range(0, len(log), 2))
        return unit_price * -(-max(0, t-basic_t) // unit_t) + base_price
    
    log = defaultdict(list)
    
    for record in records: # 1 : ์ฐจ ๋ฒˆํ˜ธ๋ณ„๋กœ ๋กœ๊ทธ ์ทจํ•ฉ (defaultdict(list))
        t, n, _ = record.split()
        h, m = map(int, t.split(':'))
        log[n].append(60*h+ m)
    
    return [price(log[n], fees) for n in sorted(log)] # 3 : ์ฐจ ๋ฒˆํ˜ธ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์„œ ๋ฐ˜ํ™˜
  • 1 : ์ฐจ ๋ฒˆํ˜ธ๋ณ„๋กœ ์ด์šฉ ์‹œ๊ฐ„ ๋กœ๊ทธ๋ฅผ ์ทจํ•ฉํ•œ๋‹ค.
  • 2 : ๋กœ๊ทธ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ด์šฉ ์‹œ๊ฐ„์„ ๊ณ„์‚ฐํ•œ๋‹ค
    • ๋กœ๊ทธ ์ˆ˜๊ฐ€ ์ง์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฉด ์ถœ์ฐจ๊ฐ€ ๋ชจ์ž๋ฅด๋‹ค๋Š” ๊ฒƒ์ด๋ฏ€๋กœ 23:59๋ถ„ ์ถœ์ฐจ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.
    • ์ด์šฉ ์‹œ๊ฐ„์„ ๋ˆ„์  ํ•ฉ์‚ฐํ•œ๋‹ค.
    • ์˜ฌ๋ฆผ์„ ์ ์šฉํ•ด ์š”๊ธˆ์„ ๊ณ„์‚ฐํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • 3 : ์ฐจ๋Ÿ‰ ๋ฒˆํ˜ธ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ๋ฌธ์ œ๋ฅผ ์ฒ˜์Œ์— ์ž˜๋ชป ์ดํ•ดํ•˜๊ณ , ๋ˆ„์ ํ•ฉ ํ›„ ๊ณ„์‚ฐํ•ด์•ผํ•˜๋Š”๊ฑธ ๋’ค๋Šฆ๊ฒŒ ์•Œ์•„์„œ ํ‘ธ๋Š”๋ฐ ์‹œ๊ฐ„์ด ๋งŽ์ด๊ฑธ๋ ธ๋‹ค.
  • ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๊ฒจ์šฐ๊ฒจ์šฐ ์‚ฌ์šฉํ•ด ํ’€์–ด๋ƒˆ๋Š”๋ฐ, ๋‹ค๋ฅธ ํ’€์ด๋ฅผ ๋ณด๋‹ˆ ๋„ˆ๋ฌด ๊น”๋”ํ•˜๊ฒŒ ํ’€์–ด์„œ ์กฐ๊ธˆ ์‹œ๋ฌด๋ฃฉํ•ด์กŒ๋‹ค.
    • -(-max(0, t-basic-t) // unit_t) ๋ถ€๋ถ„์—์„œ ์˜ฌ๋ฆผ์„ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š”๊ฑธ ๋ฐฐ์› ๋‹ค.
      ( //๋Š” ๋‚ด๋ฆผ์ด๋ฏ€๋กœ ์Œ์ˆ˜์—์„œ ๋‚ด๋ฆผํ•ด์„œ ๋‹ค์‹œ ์–‘์ˆ˜๋กœ ๋งŒ๋“ค๋ฉด ์˜ฌ๋ฆผ์ด ๋œ๋‹ค )
  • ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋” ์ž˜ ์“ธ ์ˆ˜ ์žˆ๋„๋ก ์—ฐ์Šตํ•ด์•ผ๊ฒ ๋‹ค.

728x90