๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐˜ผ๐™ก๐™œ๐™ค๐™ง๐™ž๐™ฉ๐™๐™ข/๐™‹๐™ง๐™ค๐™œ๐™ง๐™–๐™ข๐™ข๐™š๐™ง๐™จ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 2022 KAKAO BLIND RECRUITMENT '์ฃผ์ฐจ ์š”๊ธˆ ๊ณ„์‚ฐ' ํŒŒ์ด์ฌ ํ’€์ด

by beomcoder 2023. 2. 24.
728x90
๋ฐ˜์‘ํ˜•
 

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

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

programmers.co.kr

"""
(์ „๋žต)

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

import math
def solution(fees, records):
    sales = {}
    for record in records:
        time, vnum, info = record.split()
        hour, minute = map(int, time.split(':'))
        
        if vnum in sales:
            if info == 'IN':
                sales[vnum][1:] = [hour*60 + minute, 1439]
            else:
                sales[vnum][0] += ((hour*60 + minute) - sales[vnum][1])
                sales[vnum][1:] = [0, 0]
        else:
            sales[vnum] = [0, hour*60 + minute, 1439]
        
        
    result = []
    for vnum, fare in sales.items():
        total = fare[0] + (fare[2] - fare[1])
        extra = math.ceil((total-fees[0])/fees[2])*fees[3]
        fee = fees[1] + (extra if extra > 0 else 0)
        result.append([vnum, fee])
    
    return [v[1] for v in sorted(result, key=lambda x: x[0])]


"""
๋ฌธ์ œ๋Œ€๋กœ ํ’€์–ด์„œ ์นด์นด์˜ค์—์„œ ์›ํ•˜๋Š” ๋ฐฉ์‹์ธ์ง€๋Š” ๋ชจ๋ฅด๊ฒ ๋‹ค.
๋จผ์ € records๋ฅผ ๋ณด๊ธฐ์ข‹๊ฒŒ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•ด for๋ฌธ์„ ํ•œ๋ฒˆ ์‚ฌ์šฉํ–ˆ๋‹ค.

records๋ฅผ time๊ณผ vnum (vehicle number) in/out ์—ฌ๋ถ€๋กœ splitํ•˜์˜€๋‹ค.
์ŠคํŠธ๋งํ˜•์ธ ์‹œ๊ฐ„์„ ๋ถ„์œผ๋กœ ์ „๋ถ€ ๋งž์ถ”๊ธฐ ์œ„ํ•ด ๋‹ค์‹œ time์„ hour, minute์œผ๋กœ ๋‚˜๋ˆ„์—ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ฐจ๋Ÿ‰๋ฒˆํ˜ธ(vnum)๊ฐ€ ๋งค์ถœ์žฅ๋ถ€(sales)์— ์žˆ์„๋•Œ์™€ ์—†์„๋•Œ๋ฅผ ์ฒดํฌํ•œ๋‹ค.
if else๋กœ ์žˆ์„๋•Œ๋ฅผ ๋จผ์ € ๋น„๊ตํ•˜์ง€๋งŒ, ์—†์„๋•Œ๊ฐ€ ๊ฐ€์žฅ ๋จผ์ € ์ƒ๊ธฐ๋ฏ€๋กœ
else๋ฌธ๋ถ€ํ„ฐ ์„ค๋ช…ํ•˜๋ฉด ๋งค์ถœ์žฅ๋ถ€์— ์ฐจ๋Ÿ‰๋ฒˆํ˜ธ๋ฅผ ์ ๋Š”๋‹ค.
๋งค์ถœ์žฅ๋ถ€์—๋Š” [๋ช‡๋ถ„๋™์•ˆ ์ฃผ์ฐจํ–ˆ๋Š”์ง€, ๋“ค์–ด์˜จ ์‹œ๊ฐ„, ๋‚˜๊ฐ„์‹œ๊ฐ„] ์œผ๋กœ ์ ์–ด์ค€๋‹ค.

์ฒ˜์Œ์—๋Š” ์•„์ง ๋‚˜๊ฐ€์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— [0, .., ..] ์ด๊ณ 
๋“ค์–ด์˜จ ์‹œ๊ฐ„์„ ๋ถ„์œผ๋กœ ํ†ต์ผํ•˜์—ฌ [.., hour*60+minute, ...]์œผ๋กœ ์ ์—ˆ๊ณ ,
๋‚˜๊ฐ„ ์‹œ๊ฐ„์€ ๋ฌธ์ œ์—์„œ ์ ํ˜€์žˆ์ง€ ์•Š๋‹ค๋ฉด default๊ฐ€ 23:59์ด๊ธฐ ๋•Œ๋ฌธ์—
๊ณ„์‚ฐํ•˜์—ฌ [.., .., 1439(23*60+59)]๋กœ ์ ์–ด์ฃผ์—ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  if๋ฌธ์œผ๋กœ ๋Œ์•„์™€์„œ ๋งค์ถœ์žฅ๋ถ€(sales)์— ์ฐจ๋Ÿ‰๋ฒˆํ˜ธ๊ฐ€ ์žˆ๋‹ค๋ฉด
๋“ค์–ด์˜จ๊ฑด์ง€ ๋‚˜๊ฐ„๊ฑด์ง€๋ฅผ ํ™•์ธํ•œ๋‹ค.

๋“ค์–ด์˜จ๊ฑฐ๋ผ๋ฉด else๋ฌธ์— ์ ์€ ๊ฒƒ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ ์–ด์ค€๋‹ค.
์ด๋•Œ๋Š” ๋ช‡๋ถ„๋™์•ˆ ์ฃผ์ฐจํ–ˆ๋Š”์ง€ ์‹œ๊ฐ„์ด ์ ํ˜€ ์žˆ์–ด [@, hour*60+minute, 1439]๋กœ ๋œ๋‹ค.

๋‚˜๊ฐ„๊ฒƒ์ด๋ผ๋ฉด ์ถœ์ฐจ์‹œ๊ฐ„์—์„œ ์ž…์ฐจ์‹œ๊ฐ„์„ ๋นผ์ฃผ์–ด ์ฃผ์ฐจํ•œ ์‹œ๊ฐ„์— ๋„ฃ์–ด์ฃผ๊ณ 
[@, 0, 0]์œผ๋กœ ์„ธํŒ…ํ•ด์ค€๋‹ค.

for๋ฌธ์„ ๋‹ค ๋๋‚ด๊ณ  ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋‹ค.
๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค๋•Œ ์˜ˆ์™ธ๋กœ ์ž…์ฐจ์‹œ๊ฐ„๋งŒ ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์–ด
ํ•œ๋ฒˆ๋” ์ถœ์ฐจ์‹œ๊ฐ„์—์„œ ์ž…์ฐจ์‹œ๊ฐ„์„ ๋นผ์„œ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์—ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  fees์—์„œ ๊ธฐ๋ณธ์‹œ๊ฐ„๊ณผ ์š”๊ธˆ๋“ฑ์„ ๋ฌธ์ œ์—์„œ ๋‚˜์˜จ ์‹๋Œ€๋กœ ์ฒ˜๋ฆฌํ•˜์˜€๋‹ค.
๋งˆ์ง€๋ง‰์— ๋ฆฌ์ŠคํŠธ๋ฅผ ์ฐจ๋Ÿ‰๋ฒˆํ˜ธ๋กœ ์†ŒํŠธํ•˜์—ฌ ์š”๊ธˆ๋งŒ ๋ฆฌํ„ด์‹œ์ผœ์ฃผ์—ˆ๋‹ค.
"""
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€