티스토리 뷰

<나의 풀이>

- 2중for문이 있다. 최대한 줄이는게 좋겠다.

<오답노트>

- 딕셔너리 사용법

import math
n_time = {}

def car_f(n,f):
    global n_time
    if n in n_time:
        n_time[n] = n_time[n] + f
    else:
        n_time[n] = f

def solution(fees, records):
    answer = []
    at, af, at2, af2 = fees
    
    for i in range(len(records)):
        record = records[i]
        t, n, w = record.split()
        h, m = map(int,t.split(':'))
        if w == 'IN':
            for j in range(len(records)):
                record2 = records[j]
                t2, n2, w2 = record2.split()
                h2, m2 = map(int,t2.split(':'))
                if n == n2 and w2 == 'OUT' and i<=j:
                    car_f(int(n),(h2*60+m2)-(h*60+m))
                    break
                if record2 == records[-1] and n2 != 'OUT':  
                    car_f(int(n),(23-h)*60 + (59-m))

    for num,NT in n_time.items():
        if NT-at <= 0:
            n_time[num] = af
        else:
            n_time[num] = af + (math.ceil((NT-at)/at2) * af2)
            
    r = list(dict(sorted(n_time.items())).values())
    return r

 

 

<다른 사람 풀이>

- for문을 돌면서 딕셔너리에 total_time, time, status 를 저장한다.

import math

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

    for record in records:
        time, number, status = record.split()
        time = time.split(':')
        time = int(time[0])*60 + int(time[1])
        if number not in check:
            check[number] = (0, time, status)
        if status == 'IN':
            check[number] = (check[number][0], time, status)
        elif status == 'OUT':
            total_time, in_time, _ = check[number]
            total_time += time - in_time
            check[number] = (total_time, time, status)

    result = {}

    for number in check.keys():
        total_time, time, status = check[number]
        if status == 'IN':
            total_time += 1439 - time
        fee = fees[1]
        if total_time <= fees[0]:
            result[number] = fee
        else:
            fee = fee + math.ceil((total_time - fees[0]) / fees[2]) * fees[-1]
            result[number] = fee

    return list(map(lambda x : x[1], sorted(result.items())))
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함