๐Ÿถ Programming/์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€๊ธฐ

2022 KAKAO BLIND RECRUITEMENT Lv2. ์ฃผ์ฐจ์š”๊ธˆ ๊ณ„์‚ฐ

์ง€ ์› 2022. 7. 7. 00:12

2022  KAKAO BLIND RECRUITEMENT 

Lv2. ์ฃผ์ฐจ์š”๊ธˆ ๊ณ„์‚ฐ

 

๋ฌธ์ œ : https://programmers.co.kr/learn/courses/30/lessons/92341

 

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

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

programmers.co.kr

 

๐Ÿ”ธ ๋‚ด๊ฐ€ ์ƒ๊ฐํ•œ ์ ‘๊ทผ ๋ฐฉ๋ฒ•

์ž…/์ถœ์ฐจ ํ–ˆ๋˜ ์ฐจ๋“ค์˜ ๋ฒˆํ˜ธ๋กœ dictionary๋ฅผ ๋จผ์ € ๋งŒ๋“ฆ

์ž…/์ถœ์ฐจ ์ •๋ณด๋ฅผ ๋จผ์ € ๋ฐ›์•„์„œ IN์ธ ๊ฒฝ์šฐ์—” ๋‹ค์Œ OUT์„ ์ฐพ์•„์„œ ์‹œ๊ฐ„์„ ๊ณ„์‚ฐํ•จ

๋งŒ์•ฝ ๋‹ค์Œ OUT์ด ์กด์žฌ ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด 23:59์— OUT์ด๋ผ๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ๊ณ„์‚ฐ

์‹œ๊ฐ„์€ ๋‹จ์œ„์‹œ๊ฐ„์„ ์ดˆ๊ณผํ•˜์—ฌ ๋‚˜๋จธ์ง€๊ฐ€ ์ƒ๊ธฐ๋Š” ๊ฒฝ์šฐ +1 ํ•ด์คŒ

 

#์ฐจ๊ฐ€ ์žˆ๋˜ ์‹œ๊ฐ„๊ณ„์‚ฐ
def cal_fee(i,j):
    time = (int(j.split(':')[0])*60+int(j.split(':')[1]))-(int(i.split(':')[0])*60+int(i.split(':')[1]))
    return time

def solution(fees, records):
    id_list = []
    for i in records:
        id_list.append(i.split()[1])
    id_list.sort(key=int)
    car = []
    for i in id_list:
        if i not in car:
            car.append(i)

    id = { x:0 for x in car }
		
#์ž…์ถœ์ฐจ ๊ธฐ๋ก์œผ๋กœ ์‹œ๊ฐ„ dictionaty์— ์ž…๋ ฅ
    for i in records:
        if i.split()[2] == "IN":
            num = i.split()[1]
            check = 0
            for j in range(records.index(i)+1, len(records)+1):
                if records[j-1].split()[2] ==  "OUT" and records[j-1].split()[1]==i.split()[1]:
                    time = cal_fee(i.split()[0],records[j-1].split()[0])
                    check += 1
                    id[i.split()[1]] += time
                    break
            if check == 0:
                time = cal_fee(i.split()[0],"23:59")
                id[i.split()[1]] += time

#์‹œ๊ฐ„์„ ํ†ตํ•ด ์ฃผ์ฐจ๊ฐ’ ๊ณ„์‚ฐ
    answer = []
    for i in car:
        time = int(id["{}".format(i)])
        if int(time) <= fees[0]:
            answer.append(fees[1])
        else:
            if (int(time)-fees[0])%fees[2] == 0:
                a = 0
            else:
                a = 1
            total = fees[1] + ((int(time)-fees[0])//fees[2]+a)*fees[3]
            answer.append(total)
    return answer

๐Ÿ”‘ Key Point

๐Ÿ”ธ set์„ ์‚ฌ์šฉํ•˜๋ฉด ์ž๋ฃŒํ˜•์œผ๋กœ ๋ฐ”๋€Œ์–ด์„œ ์ˆœ์„œ๊ฐ€ ์—‰๋ง์ด ๋œ๋‹ค.. ์ˆœ์„œ๊ฐ€ ์ค‘์š”ํ•œ ์ฝ”๋“œ๋ผ๋ฉด ์ค‘๋ณต์€ ๊ทธ๋ƒฅ for๋ฌธ์„ ํ†ตํ•ด ์ œ๊ฑฐํ•˜์ž!

๐Ÿ”ธ ๋ฌธ์ œ๋Š” ๋ˆ„์  ์‹œ๊ฐ„์„ ๊ตฌํ•˜๋Š” ๊ฑฐ์˜€๋Š”๋ฐ ๋‚˜๋Š” ๋‚˜๊ฐˆ ๋•Œ๋งˆ๋‹ค ๊ณ„์‚ฐํ•˜๋Š” ๊ฑธ๋กœ ํ–ˆ๋‹ค.. ๋ฌธ์ œ ์ž˜ ๋ณด์ž!

๐Ÿ”ธ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ฆฌ ⇒ list.sort() ; ์˜ค๋ฆ„์ฐจ์ˆœ , sort(reverse = True) ; ๋‚ด๋ฆผ์ฐจ์ˆœ , sort(key = int) ; ์ˆซ์ž๋น„๊ต!

๐Ÿ”ธ ์ž…๋ ฅ๊ฐ’์„ ๋ฌธ์ž์—ด๋กœ ๋ฐ›์•˜์œผ๋‹ˆ๊นŒ ๊ณ„์‚ฐํ•  ๋•Œ ์ •์ˆ˜ํ˜•์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š”๊ฑฐ ์žŠ์ง€๋ง๊ธฐ!!

๐Ÿ”ธ dictionary ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ ⇒ dictionary[”key๊ฐ’”]