๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ

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

๐ŸŽฎinspirer9 2023. 2. 27. 10:34
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์„ค๋ช…

์ฃผ์ฐจ์žฅ์˜ ์š”๊ธˆํ‘œ์™€ ์ฐจ๋Ÿ‰์ด ๋“ค์–ด์˜ค๊ณ (์ž…์ฐจ) ๋‚˜๊ฐ„(์ถœ์ฐจ) ๊ธฐ๋ก์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ฐจ๋Ÿ‰๋ณ„๋กœ ์ฃผ์ฐจ ์š”๊ธˆ์„ ๊ณ„์‚ฐํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” ํ•˜๋‚˜์˜ ์˜ˆ์‹œ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

  • ์š”๊ธˆํ‘œ

๊ธฐ๋ณธ ์‹œ๊ฐ„(๋ถ„)๊ธฐ๋ณธ ์š”๊ธˆ(์›)๋‹จ์œ„ ์‹œ๊ฐ„(๋ถ„)๋‹จ์œ„ ์š”๊ธˆ(์›)

180 5000 10 600

 

  • ์ž…/์ถœ์ฐจ ๊ธฐ๋ก

์‹œ๊ฐ(์‹œ:๋ถ„)์ฐจ๋Ÿ‰ ๋ฒˆํ˜ธ๋‚ด์—ญ

05:34 5961 ์ž…์ฐจ
06:00 0000 ์ž…์ฐจ
06:34 0000 ์ถœ์ฐจ
07:59 5961 ์ถœ์ฐจ
07:59 0148 ์ž…์ฐจ
18:59 0000 ์ž…์ฐจ
19:09 0148 ์ถœ์ฐจ
22:59 5961 ์ž…์ฐจ
23:00 5961 ์ถœ์ฐจ

 

  • ์ž๋™์ฐจ๋ณ„ ์ฃผ์ฐจ ์š”๊ธˆ

์ฐจ๋Ÿ‰ ๋ฒˆํ˜ธ๋ˆ„์  ์ฃผ์ฐจ ์‹œ๊ฐ„(๋ถ„)์ฃผ์ฐจ ์š”๊ธˆ(์›)

0000 34 + 300 = 334 5000 + ⌈(334 - 180) / 10⌉ x 600 = 14600
0148 670 5000 +⌈(670 - 180) / 10⌉x 600 = 34400
5961 145 + 1 = 146 5000
  • ์–ด๋–ค ์ฐจ๋Ÿ‰์ด ์ž…์ฐจ๋œ ํ›„์— ์ถœ์ฐจ๋œ ๋‚ด์—ญ์ด ์—†๋‹ค๋ฉด, 23:59์— ์ถœ์ฐจ๋œ ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค.
    • 0000๋ฒˆ ์ฐจ๋Ÿ‰์€ 18:59์— ์ž…์ฐจ๋œ ์ดํ›„, ์ถœ์ฐจ๋œ ๋‚ด์—ญ์ด ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, 23:59์— ์ถœ์ฐจ๋œ ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค.
  • 00:00๋ถ€ํ„ฐ 23:59๊นŒ์ง€์˜ ์ž…/์ถœ์ฐจ ๋‚ด์—ญ์„ ๋ฐ”ํƒ•์œผ๋กœ ์ฐจ๋Ÿ‰๋ณ„ ๋ˆ„์  ์ฃผ์ฐจ ์‹œ๊ฐ„์„ ๊ณ„์‚ฐํ•˜์—ฌ ์š”๊ธˆ์„ ์ผ๊ด„๋กœ ์ •์‚ฐํ•ฉ๋‹ˆ๋‹ค.
  • ๋ˆ„์  ์ฃผ์ฐจ ์‹œ๊ฐ„์ด ๊ธฐ๋ณธ ์‹œ๊ฐ„์ดํ•˜๋ผ๋ฉด, ๊ธฐ๋ณธ ์š”๊ธˆ์„ ์ฒญ๊ตฌํ•ฉ๋‹ˆ๋‹ค.
  • ๋ˆ„์  ์ฃผ์ฐจ ์‹œ๊ฐ„์ด ๊ธฐ๋ณธ ์‹œ๊ฐ„์„ ์ดˆ๊ณผํ•˜๋ฉด, ๊ธฐ๋ณธ ์š”๊ธˆ์— ๋”ํ•ด์„œ, ์ดˆ๊ณผํ•œ ์‹œ๊ฐ„์— ๋Œ€ํ•ด์„œ ๋‹จ์œ„ ์‹œ๊ฐ„ ๋งˆ๋‹ค ๋‹จ์œ„ ์š”๊ธˆ์„ ์ฒญ๊ตฌํ•ฉ๋‹ˆ๋‹ค.
    • ์ดˆ๊ณผํ•œ ์‹œ๊ฐ„์ด ๋‹จ์œ„ ์‹œ๊ฐ„์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€์ง€ ์•Š์œผ๋ฉด, ์˜ฌ๋ฆผํ•ฉ๋‹ˆ๋‹ค.
    • ⌈a⌉ : a๋ณด๋‹ค ์ž‘์ง€ ์•Š์€ ์ตœ์†Œ์˜ ์ •์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์˜ฌ๋ฆผ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

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

์ œํ•œ ์‚ฌํ•ญ

  • fees์˜ ๊ธธ์ด = 4
    • fees[0] = ๊ธฐ๋ณธ ์‹œ๊ฐ„(๋ถ„)
    • 1 ≤ fees[0] ≤ 1,439
    • fees[1] = ๊ธฐ๋ณธ ์š”๊ธˆ(์›)
    • 0 ≤ fees[1] ≤ 100,000
    • fees[2] = ๋‹จ์œ„ ์‹œ๊ฐ„(๋ถ„)
    • 1 ≤ fees[2] ≤ 1,439
    • fees[3] = ๋‹จ์œ„ ์š”๊ธˆ(์›)
    • 1 ≤ fees[3] ≤ 10,000
  • 1 ≤ records์˜ ๊ธธ์ด ≤ 1,000
    • records์˜ ๊ฐ ์›์†Œ๋Š” "์‹œ๊ฐ ์ฐจ๋Ÿ‰๋ฒˆํ˜ธ ๋‚ด์—ญ" ํ˜•์‹์˜ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
    • ์‹œ๊ฐ, ์ฐจ๋Ÿ‰๋ฒˆํ˜ธ, ๋‚ด์—ญ์€ ํ•˜๋‚˜์˜ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
    • ์‹œ๊ฐ์€ ์ฐจ๋Ÿ‰์ด ์ž…์ฐจ๋˜๊ฑฐ๋‚˜ ์ถœ์ฐจ๋œ ์‹œ๊ฐ์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, HH:MM ํ˜•์‹์˜ ๊ธธ์ด 5์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
      • HH:MM์€ 00:00๋ถ€ํ„ฐ 23:59๊นŒ์ง€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
      • ์ž˜๋ชป๋œ ์‹œ๊ฐ("25:22", "09:65" ๋“ฑ)์€ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    • ์ฐจ๋Ÿ‰๋ฒˆํ˜ธ๋Š” ์ž๋™์ฐจ๋ฅผ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•œ, `0'~'9'๋กœ ๊ตฌ์„ฑ๋œ ๊ธธ์ด 4์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
    • ๋‚ด์—ญ์€ ๊ธธ์ด 2 ๋˜๋Š” 3์ธ ๋ฌธ์ž์—ด๋กœ, IN ๋˜๋Š” OUT์ž…๋‹ˆ๋‹ค. IN์€ ์ž…์ฐจ๋ฅผ, OUT์€ ์ถœ์ฐจ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
    • records์˜ ์›์†Œ๋“ค์€ ์‹œ๊ฐ์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋˜์–ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
    • records๋Š” ํ•˜๋ฃจ ๋™์•ˆ์˜ ์ž…/์ถœ์ฐจ๋œ ๊ธฐ๋ก๋งŒ ๋‹ด๊ณ  ์žˆ์œผ๋ฉฐ, ์ž…์ฐจ๋œ ์ฐจ๋Ÿ‰์ด ๋‹ค์Œ๋‚  ์ถœ์ฐจ๋˜๋Š” ๊ฒฝ์šฐ๋Š” ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    • ๊ฐ™์€ ์‹œ๊ฐ์—, ๊ฐ™์€ ์ฐจ๋Ÿ‰๋ฒˆํ˜ธ์˜ ๋‚ด์—ญ์ด 2๋ฒˆ ์ด์ƒ ๋‚˜ํƒ€๋‚ด์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    • ๋งˆ์ง€๋ง‰ ์‹œ๊ฐ(23:59)์— ์ž…์ฐจ๋˜๋Š” ๊ฒฝ์šฐ๋Š” ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    • ์•„๋ž˜์˜ ์˜ˆ๋ฅผ ํฌํ•จํ•˜์—ฌ, ์ž˜๋ชป๋œ ์ž…๋ ฅ์€ ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
      • ์ฃผ์ฐจ์žฅ์— ์—†๋Š” ์ฐจ๋Ÿ‰์ด ์ถœ์ฐจ๋˜๋Š” ๊ฒฝ์šฐ
      • ์ฃผ์ฐจ์žฅ์— ์ด๋ฏธ ์žˆ๋Š” ์ฐจ๋Ÿ‰(์ฐจ๋Ÿ‰๋ฒˆํ˜ธ๊ฐ€ ๊ฐ™์€ ์ฐจ๋Ÿ‰)์ด ๋‹ค์‹œ ์ž…์ฐจ๋˜๋Š” ๊ฒฝ์šฐ

์ž…์ถœ๋ ฅ ์˜ˆ

fees records result
[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"] [14600, 34400, 5000]
[120, 0, 60, 591] ["16:00 3961 IN","16:00 0202 IN","18:00 3961 OUT","18:00 0202 OUT","23:58 3961 IN"] [0, 591]
[1, 461, 1, 10] ["00:00 1234 IN"] [14841]

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1

๋ฌธ์ œ ์˜ˆ์‹œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • ์š”๊ธˆํ‘œ

๊ธฐ๋ณธ ์‹œ๊ฐ„(๋ถ„)๊ธฐ๋ณธ ์š”๊ธˆ(์›)๋‹จ์œ„ ์‹œ๊ฐ„(๋ถ„)๋‹จ์œ„ ์š”๊ธˆ(์›)

120 0 60 591
  • ์ž…/์ถœ์ฐจ ๊ธฐ๋ก

์‹œ๊ฐ(์‹œ:๋ถ„)์ฐจ๋Ÿ‰ ๋ฒˆํ˜ธ๋‚ด์—ญ

16:00 3961 ์ž…์ฐจ
16:00 0202 ์ž…์ฐจ
18:00 3961 ์ถœ์ฐจ
18:00 0202 ์ถœ์ฐจ
23:58 3961 ์ž…์ฐจ
  • ์ž๋™์ฐจ๋ณ„ ์ฃผ์ฐจ ์š”๊ธˆ

์ฐจ๋Ÿ‰ ๋ฒˆํ˜ธ๋ˆ„์  ์ฃผ์ฐจ ์‹œ๊ฐ„(๋ถ„)์ฃผ์ฐจ ์š”๊ธˆ(์›)

0202 120 0
3961 120 + 1 = 121 0 +⌈(121 - 120) / 60⌉x 591 = 591
  • 3961๋ฒˆ ์ฐจ๋Ÿ‰์€ 2๋ฒˆ์งธ ์ž…์ฐจ๋œ ํ›„์—๋Š” ์ถœ์ฐจ๋œ ๋‚ด์—ญ์ด ์—†์œผ๋ฏ€๋กœ, 23:59์— ์ถœ์ฐจ๋˜์—ˆ๋‹ค๊ณ  ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #3

  • ์š”๊ธˆํ‘œ

๊ธฐ๋ณธ ์‹œ๊ฐ„(๋ถ„)๊ธฐ๋ณธ ์š”๊ธˆ(์›)๋‹จ์œ„ ์‹œ๊ฐ„(๋ถ„)๋‹จ์œ„ ์š”๊ธˆ(์›)

1 461 1 10
  • ์ž…/์ถœ์ฐจ ๊ธฐ๋ก

์‹œ๊ฐ(์‹œ:๋ถ„)์ฐจ๋Ÿ‰ ๋ฒˆํ˜ธ๋‚ด์—ญ

00:00 1234 ์ž…์ฐจ
  • ์ž๋™์ฐจ๋ณ„ ์ฃผ์ฐจ ์š”๊ธˆ

์ฐจ๋Ÿ‰ ๋ฒˆํ˜ธ๋ˆ„์  ์ฃผ์ฐจ ์‹œ๊ฐ„(๋ถ„)์ฃผ์ฐจ ์š”๊ธˆ(์›)

1234 1439 461 +⌈(1439 - 1) / 1⌉x 10 = 14841
  • 1234๋ฒˆ ์ฐจ๋Ÿ‰์€ ์ถœ์ฐจ ๋‚ด์—ญ์ด ์—†์œผ๋ฏ€๋กœ, 23:59์— ์ถœ์ฐจ๋˜์—ˆ๋‹ค๊ณ  ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค.

ํ’€์ด

์นด์นด์˜ค ๋ฌธ์ œ ๋‹ต์ง€ ์•Š๊ฒŒ ์นœ์ ˆํ•œ ๋ฌธ์ œ ใ…‹ใ…‹ใ…‹

import math

def solution(fees, records):
    answer = []    
    parking_in_out = {}
    parking_record = []
    
    for i in range(len(records)): # ์‹œ๊ฐ„ ์ •๋ฆฌ
        hour = int(records[i][:2])
        mins = int(records[i][3:5])
        carn = records[i][6:10]
        if carn in parking_in_out:
            parking_in_out[carn] += 1
        else:
            parking_in_out[carn] = 1
        parking_record.append([carn, (hour * 60 + mins)])
    
    for dic in parking_in_out.items(): # ์ถœ์ฐจ ์—†๋Š” ์• ๋“ค ์ถ”๊ฐ€
        if dic[1] % 2 == 1:
            parking_record.append([dic[0], 23 * 60 + 59]) 
            parking_in_out[dic[0]] += 1
    
    parking_record.sort(key=lambda x:(x[0],x[1])) # ์ฐจ๋Ÿ‰ ๋ฒˆํ˜ธ ์ˆœ ์ •๋ ฌ
    
    i = 0
    while i < len(parking_record): # ์š”์ฆ˜ ์ •์‚ฐ
        fee, time = 0, 0
        for ii in range(0,parking_in_out[parking_record[i][0]],2):
            time += parking_record[i+ii+1][1]-parking_record[i+ii][1]
        if time <= fees[0]:
            fee = fees[1]
        else:
            time -= fees[0]
            fee = fees[1] + math.ceil(time / fees[2]) * fees[3]
        answer.append(fee)
        i += parking_in_out[parking_record[i][0]]
        
    return answer

๋ฌธ์ œ๋Š” ์–ด๋ ต์ง€ ์•Š์•˜๋Š”๋ฐ ์ „์ฒ˜๋ฆฌ๊ฐ€ ์˜ค๋ž˜ ๊ฑธ๋ ธ๋‹ค.

์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.4MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.08ms, 10.4MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.25ms, 10.4MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.29ms, 10.4MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (2.20ms, 10.7MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (1.47ms, 10.6MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.27ms, 10.4MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (1.89ms, 10.7MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (2.76ms, 10.7MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (2.96ms, 10.7MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.4MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.3MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.4MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.4MB)
  • ์ฝ”๋“œ ์Šคํƒ€์ผ์ด ๋จผ์ง€๋‹ค๊ณ  ์ถ”์ฒœ์„ ๋งŽ์ด ๋ฐ›์€ ์ฝ”๋“œ
from collections import defaultdict
from math import ceil

class Parking:
    def __init__(self, fees):
        self.fees = fees
        self.in_flag = False
        self.in_time = 0
        self.total = 0

    def update(self, t, inout):
        self.in_flag = True if inout=='IN' else False
        if self.in_flag:  self.in_time = str2int(t)
        else:             self.total  += (str2int(t)-self.in_time)

    def calc_fee(self):
        if self.in_flag: self.update('23:59', 'out')
        add_t = self.total - self.fees[0]
        return self.fees[1] + ceil(add_t/self.fees[2]) * self.fees[3] if add_t >= 0 else self.fees[1]

def str2int(string):
    return int(string[:2])*60 + int(string[3:])

def solution(fees, records):
    recordsDict = defaultdict(lambda:Parking(fees))
    for rcd in records:
        t, car, inout = rcd.split()
        recordsDict[car].update(t, inout)
    return [v.calc_fee() for k, v in sorted(recordsDict.items())]
  • ๋‹ค๋ฅธ ์ฝ”๋“œ ์—ญ์‹œ ๋Œ€๋ถ€๋ถ„ ๋น„์Šทํ•˜๊ฒŒ ์ฒ˜๋ฆฌ, ๋”ฑํžˆ ์–ด๋ ค์›€ ์—†์—ˆ๋˜ ๋ฌธ์ œ
728x90
๋ฐ˜์‘ํ˜•