<๋ฌธ์ ๋งํฌ>
https://school.programmers.co.kr/learn/courses/30/parts/12081
<ํ์ด ์ ๋ต>
1. ๊ฐ์ ์ซ์๋ ์ซ์ด: ์คํ์ ํ์ฉํด์ ํ์๋ค.
2. ์ฌ๋ฐ๋ฅธ ๊ดํธ: ๋ง์ฐฌ๊ฐ์ง๋ก ์คํ์ ํ์ฉํด์ ํ์๋ค
3. ๊ธฐ๋ฅ ๊ฐ๋ฐ: ์ ๋ ฅ๊ฐ์ ํ๋ก ๋ณํํด์ ์๋ฃ ๋ ๋๋ง๋ค ํ๋์ฉ ๋นผ๊ฐ๋ฉด์ ํ์๋ค.
4. ํ๋ฆฐํฐ: enumerate๋ ํ๋ฅผ ํ์ฉํด์ ์์ฒญ ๋ฌธ์ ๋ฒํธ๊ฐ ๋์ฌ ๋๊น์ง ํ๋ฅผ ๋๋ ค์ ํ์๋ค.
5. ๋ค๋ฆฌ๋ฅผ ์ง๋๋ ํธ๋ญ: ๋ค๋ฆฌ๋ฅผ ์ฃผ์ด์ง ๊ธธ์ด์ ๋ง๋ ํ๋ก ๊ตฌํํด์ ํธ๋ญ์ ์ด๋์ ๊ตฌํํด์ ํ์๋ค. ์ฃผ์ํ ์ ์ผ๋ก๋ ํ๋ ๋ค๋ฆฌ์ ํ์ค์ ๊ฒ์ฌํ ๋ sum์ ์ฐ๋ฉด ํจ์จ์ฑ์ ํต๊ณผํ์ง ๋ชปํ๋ฏ๋ก ํ์ฌ ํ์ค์ ๋ฐ๋ก ๋ณ์๋ก ํ ๋นํด์ ๊ณ์ฐํด์ค์ผ ํ๋ค.
6. ์ฃผ์ ๊ฐ๊ฒฉ: 2์ค for๋ฌธ์ผ๋ก ํ์ด๋ ํต๊ณผ๊ฐ ๋๋ค. ์ฃผ์ํ ์ ์ ์ธ๋ฑ์ค ๋ฑ ๋ฒ์ ์ค์ ์ ์ํด์ค์ผ ๋๋ค.
<์ ๋ต ์ฝ๋>
# 1. ๊ฐ์ ์ซ์๋ ์ซ์ด
def solution(arr):
answer = []
for num in arr:
if answer and answer[-1] == num:
continue
answer.append(num)
return answer
# 2. ์ฌ๋ฐ๋ฅธ ๊ดํธ
def solution(s):
answer = True
stack = []
for char in s:
if not stack:
stack.append(char)
else:
if stack[-1] == '(' and char == ')':
stack.pop()
else:
stack.append(char)
if stack:
answer = False
return answer
# 3. ๊ธฐ๋ฅ๊ฐ๋ฐ
from collections import deque
def solution(progresses, speeds):
progresses = deque(progresses)
speeds = deque(speeds)
answer = []
while True:
for i in range(len(progresses)):
progresses[i] += speeds[i]
if progresses[0] >= 100:
done = 0
while True:
if not progresses:
break
if progresses[0] < 100:
break
progresses.popleft()
speeds.popleft()
done += 1
answer.append(done)
if not progresses:
break
return answer
# 4. ํ๋ฆฐํฐ
from collections import deque
def solution(priorities, location):
printer = deque(enumerate(priorities))
answer = 0
while True:
if printer[0][1] == max(printer, key=lambda x:x[1])[1]:
answer += 1
idx = printer.popleft()[0]
if idx == location:
break
else:
printer.append(printer.popleft())
return answer
# 5. ๋ค๋ฆฌ๋ฅผ ์ง๋๋ ํธ๋ญ
from collections import deque
def solution(bridge_length, weight, truck_weights):
answer = 1
bridge = deque([0 for _ in range(bridge_length)])
truck_weights = deque(truck_weights)
truck_cnt = len(truck_weights)
cnt = 0
current_weight = 0
while True:
out = bridge.popleft()
if out > 0:
cnt += 1
current_weight -= out
if cnt == truck_cnt:
break
if truck_weights and weight >= current_weight + truck_weights[0]:
in_truck = truck_weights.popleft()
bridge.append(in_truck)
current_weight += in_truck
else:
bridge.append(0)
answer += 1
return answer
# 6. ์ฃผ์๊ฐ๊ฒฉ
def solution(prices):
answer = []
for i in range(len(prices) - 1):
cnt = 1
for j in range(i + 1, len(prices) - 1):
if prices[j] < prices[i]:
break
cnt += 1
answer.append(cnt)
answer.append(0)
return answer
'โญ Problem_Solving > ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] ๊ณ ๋์ kit - ์์ ํ์ (Python/ํ์ด์ฌ) (0) | 2023.02.07 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ๊ณ ๋์ kit - ์ ๋ ฌ (Python/ํ์ด์ฌ) (0) | 2023.02.05 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๊ณ ๋์ kit - ํ(Heap) (Python/ํ์ด์ฌ) (0) | 2023.02.04 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์ฌ์น์ฐ์ฐ (Python/ํ์ด์ฌ) (0) | 2023.01.31 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๊ณ ๋์ kit - ํด์ (0) | 2023.01.14 |
๋๊ธ