โ ๋ฌธ์
๐๏ธ ์์ด๋์ด
1. ์ถ์ฐจ ๊ธฐ๋ก์ด ์๋ ์ฐจ ๋ฒํธ๋ฅผ ์ฐพ๋๋ค.
a. ์ฃผ์ฐจ์ฅ์ ์ด๋ฏธ ์๋ ์ฐจ๋(์ฐจ๋๋ฒํธ๊ฐ ๊ฐ์ ์ฐจ๋)์ด ๋ค์ ์ ์ฐจ๋๋ ๊ฒฝ์ฐ๊ฐ ์๊ธฐ ๋๋ฌธ์ ์ ์ถ์ฐจ ๊ฐ์๊ฐ ํ์๋ผ๋ฉด ์ถ์ฐจ ๊ธฐ๋ก์ด ํ ๊ฐ ๋ถ์กฑํ ๊ฒ์ด๋ค.
b. 23:39๋ก ์ถ์ฐจ ๊ธฐ๋ก์ ์ถ๊ฐํด์ค๋ค.
2. ์ฐจ๋ ๋ฒํธ - ์๊ฐ - ๋ด์ญ ์์ผ๋ก ์ ๋ ฌ์ ํ๋ค.
a. ๋ด์ญ ์ ๋ ฌ์ ํ๋ฉด ์ํ๋ฒณ ์์ผ๋ก IN - OUT์ด ์ ๋ ฌ๋๋ค.
3. ์ฐจ๋ ๋ณ ์ฃผ์ฐจ ๋์ ์๊ฐ์ ๊ณ์ฐํ๋ค.
a. ์ถ์ฐจ ์๊ฐ์ธ ๊ฒฝ์ฐ ์ ์ธ๋ฑ์ค์ ์๊ฐ(์ ์ฐจ)์ ์ฐจ์ด๋ฅผ ๊ตฌํด์ ๋ํด์ค๋ค.
4. ์ต์ข ์๊ธ์ ๊ตฌํด์ค๋ค.
a. ์ฌ๋ฆผ์ ์ฌ์ฉํ๋ค๊ณ ํ๊ธฐ ๋๋ฌธ์ math๋ฅผ ์ํฌํธํด์ ceil ํจ์๋ฅผ ์ฌ์ฉํ๋ค.
๐ป ํ์ด
import math
from collections import Counter
def solution(fees, records):
default_time, default_fee, unit_time, unit_fee = fees
# ์ฐจ ๋ฒํธ ๋ณ ์
์ถ์ฐจ ๊ฐฏ์ ๊ณ์ฐ
car_numbers = dict(Counter(map(lambda x: x.split()[1], records)))
# ์ถ์ฐจ ๊ธฐ๋ก์ด ์๋ ๋ฒํธ๋ 23:59 ์ถ๊ฐ
for car in car_numbers.items():
if car[1] % 2 != 0:
records.append(f'23:59 {car[0]} OUT')
# ์ฐจ๋ ๋ฒํธ, ์๊ฐ, ๋ด์ญ ์์ผ๋ก ์ ๋ ฌ
records.sort(key=lambda x: (x.split()[1], x.split()[0], x.split()[2]))
# ['06:00 0000 IN', '06:34 0000 OUT', '18:59 0000 IN', '23:59 0000 OUT', '07:59 0148 IN', '19:09 0148 OUT', '05:34 5961 IN', '07:59 5961 OUT', '22:59 5961 IN', '23:00 5961 OUT']
# ์ฐจ๋ ๋ณ ๋์ ์๊ฐ
sum_time = {}
for index, record in enumerate(records):
time, car, history = record.split()
# ์ถ์ฐจ ์๊ฐ์ธ ๊ฒฝ์ฐ
if history == "OUT":
# ์
์ฐจ ์๊ฐ
in_time = records[index - 1].split()[0]
sub_hour = int(time.split(':')[0]) - int(in_time.split(':')[0])
sub_minute = int(time.split(':')[1]) - int(in_time.split(':')[1])
# ์ค์ ์ฃผ์ฐจ ์๊ฐ
sub_time = 60 * sub_hour + sub_minute
if car in sum_time:
sum_time[car] += sub_time
else:
sum_time[car] = sub_time
answer = []
for time in sum_time.values():
# ์ต์ข
์๊ธ
if time <= default_time:
answer.append(default_fee)
else:
answer.append(default_fee + math.ceil((time - default_time) / unit_time) * unit_fee)
return answer
print(solution([180, 5000, 10, 600], ["05:34 5961 IN", "06:00 0000 IN", "06:34 0000 OUT", "07:59 5961 OUT", "07:59 0148 IN", "18:59 0000 IN", "19:09 0148 OUT", "22:59 5961 IN", "23:00 5961 OUT"]))
'Algorithm | Language > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[programmers/Lv.2] ๋ฆฌ์ฝ์ณ ๋ก๋ด (1) | 2023.06.05 |
---|---|
[programmers/Lv.2] ๋ ํ ํฉ ๊ฐ๊ฒ ๋ง๋ค๊ธฐ (0) | 2023.03.10 |
[programmers/Lv.2] ํผ์์ ํ๋ ํฑํํ (1) | 2023.03.06 |
[programmers/Lv.2] [1์ฐจ] ์บ์ (0) | 2023.03.06 |
[programmers/Lv.2] ํ์ผ๋ช ์ ๋ ฌ (0) | 2023.02.23 |