๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

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

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ฃผ์ฐจ ์š”๊ธˆ ๊ณ„์‚ฐ (Python)

1. ๋ฌธ์ œ ์„ค๋ช…

๋ฌธ์ œ ๋งํฌ

  • ์ž…๋ ฅ
    • fees: ์ฃผ์ฐจ ์š”๊ธˆ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ ๋ฐฐ์—ด(๊ธฐ๋ณธ ์‹œ๊ฐ„, ๊ธฐ๋ณธ ์š”๊ธˆ, ๋‹จ์œ„์‹œ๊ฐ„, ๋‹จ์œ„ ์š”๊ธˆ)
    • records : ์ž๋™์ฐจ์˜ ์ž…/์ถœ์ฐจ ๋‚ด์—ญ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž์—ด ๋ฐฐ์—ด
  • ์ถœ๋ ฅ
    • ์ฐจ๋Ÿ‰ ๋ฒˆํ˜ธ๊ฐ€ ์ž‘์€ ์ž๋™์ฐจ๋ถ€ํ„ฐ ์ฒญ๊ตฌํ•  ์ฃผ์ฐจ ์š”๊ธˆ์„ ์ฐจ๋ก€๋Œ€๋กœ ์ •์ˆ˜ ๋ฐฐ์—ด์— ๋‹ด์•„์„œ return

2. ์ฝ”๋“œ

solution1.py

import math

def time_to_minutes(time):
    h, m = map(int, time.split(':'))
    return h*60 + m

def solution(fees, records):
    answer = []

    # ๊ธฐ๋ณธ ์‹œ๊ฐ„, ๊ธฐ๋ณธ ์š”๊ธˆ, ๋‹จ์œ„ ์‹œ๊ฐ„, ๋‹จ์œ„ ์š”๊ธˆ ์ˆœ
    dt, df, ut, uf = fees

    dic = dict()

    for r in records:
        time, car, inout = r.split()
        car = int(car)

        if car in dic: #์ฐจ๋ณ„๋กœ ์ฃผ์ฐจ์‹œ๊ฐ„(๋ถ„๋‹จ์œ„), inout์„ ์ €์žฅ
            dic[car].append([time_to_minutes(time), inout])
        else:
            dic[car] = [[time_to_minutes(time), inout]]

    rList = sorted(dic.items()) #key๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ด์„œ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ˜ํ™˜

    for r in rList:
        t = 0

        for rr in r[1]: #item๋“ค ํ™•์ธ
            if rr[1] == "IN": #inout๊ฐ€ IN์ด๋ฉด
                t -= rr[0] #์‹œ๊ฐ„ ๋นผ๊ธฐ
            else: #OUT ์ด๋ฉด
                t += rr[0] #์‹œ๊ฐ„ ๋”ํ•˜๊ธฐ

        if r[1][-1][1] == "IN": #๋งˆ์ง€๋ง‰์— ์ถœ์ฐจ ๋‚ด์—ญ์ด ์—†๋Š” ๊ฒฝ์šฐ
            t += time_to_minutes('23:59') #์ถœ์ฐจ์‹œ๊ฐ„์„ 23:59์œผ๋กœ ๊ฐ„์ฃผ

        if t <= dt: #๊ธฐ๋ณธ์‹œ๊ฐ„๋ณด๋‹ค ์ž‘์œผ๋ฉด ๊ธฐ๋ณธ์š”๊ธˆ์„ ์ถ”๊ฐ€
            answer.append(df)
        else: #๊ธฐ๋ณธ ์‹œ๊ฐ„์ด์ƒ์ด๋ฉด
            answer.append(df + math.ceil((t-dt) / ut) * uf)

    return answer

3. ํšŒ๊ณ 

  • ๋ˆ„์ ์‹œ๊ฐ„ ๊ณ„์‚ฐ์‹œ, ๋‘ ๋ฒˆ ์ด์ƒ ์ฃผ์ฐจํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•˜์ง€ ์•Š์Œ, ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„ ์ „์— ์˜ˆ์™ธ ์ผ€์ด์Šค ๊ผญ ์ƒ๊ฐํ•˜๊ธฐ
    • ์ž๋™์ฐจ๋ณ„๋กœ [IN, OUT, ๋‚ด์•ผํ•  ์š”๊ธˆ]์„ ๋”•์…”๋„ˆ๋ฆฌ์— ์ •๋ฆฌํ•˜๊ณ  ํ’€์—ˆ๋Š”๋ฐ, ๋‘ ๋ฒˆ ์ด์ƒ ์ฃผ์ฐจํ•˜๋Š” ๊ฒฝ์šฐ, IN, OUT ์‹œ๊ฐ„์ด ํ•˜๋‚˜์ด์ƒ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค๋Š”๊ฑธ ๋‚˜์ค‘์— ๊นจ๋‹ฌ์•˜๋‹ค. ๋‚˜๋Š” ์ด์ „ ์ •๋ณด์— ์ƒˆ๋กœ์šด ์ •๋ณด๋ฅผ ๋ฎ์–ด์”Œ์šฐ๋ฉฐ ํ’€๊ณ  ์žˆ์—ˆ๋‹ค.
    • ํ•ด๊ฒฐ ๋ฐฉ๋ฒ• : ์–ด๋–ค ์ฐจ๊ฐ€ ๋‘ ๋ฒˆ ์ด์ƒ ์ฃผ์ฐจํ•˜๋Š” ๊ฒฝ์šฐ ๋ฆฌ์ŠคํŠธ ์•ˆ์— ์ •๋ณด๋ฅผ ์ถ”๊ฐ€
      • {5961: [[334, 'IN'], [479, 'OUT'], [1379, 'IN'], [1380, 'OUT']], 0: [[360, 'IN'], [394, 'OUT'], [1139, 'IN']], 148: [[479, 'IN'], [1149, 'OUT']]}
  • dictionary์˜ key๋กœ ์ฐจ ๋ฒˆํ˜ธ๋ฅผ ๋„ฃ๊ธฐ ์ „,๋ฏธ๋ฆฌ int()๋ฅผ ์”Œ์›Œ์คฌ๋‹ค : ์ฐจ ๋ฒˆํ˜ธ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ ํ•  ๋•Œ๋ฅผ ์œ„ํ•ด
    • ๋‚˜์ค‘์— ์ตœ์ข… ์ถœ๋ ฅ์—์„œ๋„ ์ฐจ๋ฒˆํ˜ธ๊ฐ€ ๋น ๋ฅธ ์ˆœ์„œ๋Œ€๋กœ "์ฃผ์ฐจ์š”๊ธˆ"๋งŒ ์ถœ๋ ฅํ•˜๋ฉด๋˜๋‹ˆ๊นŒ ๊ตณ์ด ์ฐจ ๋ฒˆํ˜ธ๋ฅผ ์›์ƒ๋ณต๊ท€ ์‹œํ‚ฌ ํ•„์š”๊ฐ€ ์—†๋‹ค.
  • ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ key ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜๊ธฐ : rList = sorted(dic.items())
    • key๊ธฐ์ค€์œผ๋กœ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ ํ›„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ˜ํ™˜๋จ
  • ๋™์ผํ•œ ์ฐจ๋Ÿ‰์˜ IN, OUT ๋•Œ์˜ ์‹œ๊ฐ„์„ ์ถ”์ ํ•ด ๋‘˜์˜ ์‹œ๊ฐ„์ฐจ๋ฅผ ๊ตฌํ•˜๊ธฐ ๋ณด๋‹ค ๊ฐ ์‹œ๊ฐ„์„ ๋ถ„์œผ๋กœ ๋ฐ”๊พผ ์‹œ๊ฐ„์„ ์ ˆ๋Œ€์ ์ธ ์‹œ๊ฐ„์œผ๋กœ ๋ณด๊ณ , IN์ด ๋‚˜์˜ค๋ฉด time์—์„œ ๋นผ๊ณ , ๋‚˜๊ฐ€๋ฉด time์— ๋”ํ•˜๋Š” ๋ฐฉ์‹์„ ์ทจํ•œ๋‹ค.

4. ์ฐธ๊ณ 

https://velog.io/@minnseong/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4-%EC%A3%BC%EC%B0%A8-%EC%9A%94%EA%B8%88-%EA%B3%84%EC%82%B0