DevYoon
[ํ๋ก๊ทธ๋๋จธ์ค] ๋ค๋ฆฌ๋ฅผ ์ง๋๋ ํธ๋ญ (Python) ๋ณธ๋ฌธ
link ๐ https://programmers.co.kr/learn/courses/30/lessons/42583
์๊ฐ์ด๊ณผ๊ฐ ๋ฌ๋ ์ฝ๋
def solution(bridge_length, weight, truck_weights):
answer = 1
bridge = [0]*bridge_length
bridge[0] = truck_weights.pop(0)
while sum(bridge)!=0:
for idx in range(bridge_length-1, -1, -1):
if idx == bridge_length-1 and bridge[idx] != 0:
bridge[idx] = 0
elif bridge[idx] != 0:
bridge[idx+1] = bridge[idx]
bridge[idx] = 0
if len(truck_weights) > 0:
if sum(bridge)+truck_weights[0] <= weight and bridge[0] == 0:
bridge[0] = truck_weights.pop(0)
answer += 1
return answer
์์ ์ฝ๋
def solution(bridge_length, weight, truck_weights):
answer = 0
bridge = [0]*bridge_length
sum_bridge = 0
while len(bridge):
answer += 1
sum_bridge -= bridge[0]
bridge.pop(0)
if len(truck_weights):
if sum_bridge+truck_weights[0] <= weight:
bridge.append(truck_weights.pop(0))
sum_bridge += bridge[-1]
else:
bridge.append(0)
return answer
โ๏ธ ๋ฉ๋ชจ
- ํ ์คํธ์ผ์ด์ค 5๋ฒ์์ ๊ณ์ ์๊ฐ์ด๊ณผ๊ฐ ๋ฌ๋ค.
- ์ฌ๋ฌ ๋ฒ์ ์๋ ๋์ sum(bridge)๋ก ํฉ์ ๊ตฌํด์ค ๊ฒ ๋ฌธ์ ์ธ ๊ฒ ๊ฐ๋ค๊ณ ๋๊ผ๋ค.
- sum์ ์ฐ์ง ์๊ณ sum_bridge๋ผ๋ ๋ณ์๋ฅผ ๋ฐ๋ก ์ฌ์ฉํด์ appendํ ๋ sum_bridge์ ๊ทธ ๊ฐ์ ๋ํด์ฃผ๊ณ , pop ํ ๋ ๊ทธ ๊ฐ์ ๋นผ์ฃผ๋ ์์ผ๋ก ์์ ํ๋ค.
- ํ ์คํธ์ผ์ด์ค 5 ์๊ฐ ์ด๊ณผ ํด๊ฒฐ!