[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ๋ค๋ฆฌ๋ฅผ ์ง๋๋ ํธ๋ญ
1. ๋ฌธ์ : Link
๊ฑด๋๋๋ง๋ค 1๋งํผ ์ง๋๊ฐ๋๋ง๋ค 1์ด๊ฐ ๊ฑธ๋ฆฌ๊ณ
๋ค๋ฆฌ๊ฐ ์งํฑํ ์ ์๋ ๋ฌด๊ฒ๋งํผ์ ์ฐจ๋ง ์ง๋๋ค๋ ์ ์๋ค๊ณ ํ์๋
์ต๋จ์๊ฐ์ผ๋ก ๊ฑด๋ ๋์ ์๊ฐ
2. ํ์ด
queue๋ฅผ ์ด์ฉํด์ ๋ค๋ฆฌ์์์๋ ๋ชจ๋ ์ฐจ์ ๋ฌด๊ฒ์ ์๋ก ๋ค์ด์ค๋ ๋ฌด๊ฒ๋ฅผ ํฉํ์ฌ ๊ฐ๋น๊ฐ๋ฅํ ๋ฌด๊ฒ์ ๋น๊ตํ๋ค.
3. ์ฝ๋
def solution(bridge_length, weight, truck_weights):
bridge = [truck_weights.pop(0)]
bridge_count = [1]
time = 1
while 1:
time += 1
if not truck_weights:
time += bridge_length-1
break
else:
if bridge_count[0] == bridge_length:
bridge_count.pop(0)
bridge.pop(0)
if sum(bridge) + truck_weights[0] <= weight:
bridge.append(truck_weights.pop(0))
bridge_count.append(0)
for i in range(len(bridge_count)):
bridge_count[i] += 1
return time
์ข ๋ ๋์ ์ฝ๋
def solution(bridge_length, weight, truck_weights):
answer = 0
bridge = [0 for _ in range(bridge_length)]
while bridge:
answer += 1
bridge.pop(0)
if truck_weights:
if sum(bridge) + truck_weights[0] <= weight:
t = truck_weights.pop(0)
bridge.append(t)
else:
bridge.append(0)
return answer
'๐ป Coding Problems Solving > Stack | Queue' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ํ๋ฆฐํฐ (์๋ฐ java) (0) | 2023.02.22 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ์ฌ๋ฐ๋ฅธ ๊ดํธ (์๋ฐ java) (0) | 2022.11.25 |
[BOJ 2504] ๊ดํธ์ ๊ฐ (0) | 2022.06.26 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ์ง์ง์ด ์ ๊ฑฐํ๊ธฐ (0) | 2022.04.09 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ๋ ๋งต๊ฒ (0) | 2022.04.08 |
์ต๊ทผ๋๊ธ