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๊ฐโ]
'๐ถ Programming > ์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ํ๊ธฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
2021 ์นด์นด์ค ์ฑ์ฉ์ฐ๊ณํ ์ธํด์ญ Lv2. ๊ฑฐ๋ฆฌ๋๊ธฐ ํ์ธํ๊ธฐ (0) | 2022.09.11 |
---|---|
2021 ์นด์นด์ค ์ฑ์ฉ์ฐ๊ณํ ์ธํด์ญ Lv1. ์ซ์ ๋ฌธ์์ด๊ณผ ์๋จ์ด (0) | 2022.07.17 |
2022 KAKAO BLIND RECRUITEMENT Lv2. ์๊ถ๋ํ (0) | 2022.07.17 |
2022 KAKAO BLIND RECRUITEMENT Lv2. k์ง์์์ ์์ ๊ฐ์ ๊ตฌํ๊ธฐ (0) | 2022.07.07 |
2022 KAKAO BLIND RECRUITEMENT Lv1. ์ ๊ณ ๋ฐ๊ธฐ (0) | 2022.07.07 |