์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
- ์๊ณ ๋ฆฌ์ฆ
- ์ฝ๋ฉํ ์คํธ
- ํ ์ดํ๋ก์ ํธ
- ์ฝ๋ฉ์ ํ
- ๊ตญ๋น์ง์
- heapq
- ๋ชจ๊ฐ์ฝ
- CSS
- HTML
- node.js
- ํ์ด์ฌ
- ์ฝ๋ฉ
- ํฌ๋กค๋ง
- mongodb
- KDT
- ๋ ธ๋ง๋์ฝ๋
- ๋๋ฆผ์ฝ๋ฉ
- Til
- ๋ฐฑ์ค
- ๊ทธ๋ฆฌ๋
- Python
- ํ๋ก๊ทธ๋๋จธ์ค
- JS
- react
- error
- fe
- ํ๋ก ํธ์๋
- ํ๋ก์ ํธ
- ๊ฐ๋ฐ
- javascript
- Today
- Total
๐ฑ → ๐ณ
[ํ๋ก๊ทธ๋๋จธ์ค] ๊ธฐ๋ฅ๊ฐ๋ฐ - python ๋ณธ๋ฌธ
https://school.programmers.co.kr/learn/courses/30/lessons/42586
ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
๋ฌธ์ ์ ๋ณด
๋ฌธ์ ์ ํ : stack/queue
์ค๊ณ ๋ฐฉ๋ฒ
์์ ์๊ฐ์ ๋ฏธ๋ฆฌ ๊ณ์ฐํด๋๊ณ q์ ์ ์ฅ (q๋ deque)
q.popleftํด์ ์์ ์๋ ๊ธฐ๋ฅ์ด ๋ฐฐํฌ๋ ๋ ํจ๊ป ๋ฐฐํฌ๋ ์ ์๋ ๋ท ๊ธฐ๋ฅ ์ ์ฅ
์ฝ๋
์คํจ ์ฝ๋
from collections import deque
def solution(progresses, speeds):
answer = []
cnt = 1
q = deque()
for i in range(len(progresses)):
if (100-progresses[i]) % speeds[i] == 0:
q.append((100-progresses[i]) // speeds[i])
else:
q.append((100-progresses[i]) // speeds[i] + 1)
while q:
v = q.popleft()
if len(q) > 0:
if v >= q[0]:
cnt += 1
else:
answer.append(cnt)
cnt = 1
else:
answer.append(cnt)
return answer
ํ ์คํธ 2,3,4,5,6,7,9,10์์ ์คํจ ๋ฐ์ ์์ง ?
์ ๋ฐ๋ก ์ฐพ์์
๐ฏ๏ธ ๋ฐ๋ก) progresses : [40, 93, 30, 55, 60, 65], speeds : [60, 1, 30, 5 , 10, 7], return : [1,2,3]
์ด ๊ฒฝ์ฐ q = [1,7,3,9,4,5]
๋ด๊ฐ ์ง ์ฝ๋๋๋กํ๋ฉด result = [1,2,2,1] ๋์ด
[9,4,5] ์ฌ๊ธฐ์ 4๊ฐ 5๋ณด๋ค ์์ผ๋๊น cnt = 1๋ก ์ด๊ธฐํ ํด๋ฒ๋ฆผ
์ฝ๋ ์์
from collections import deque
def solution(progresses, speeds):
answer = []
cnt = 1
q = deque()
for i in range(len(progresses)):
if (100-progresses[i]) % speeds[i] == 0:
q.append((100-progresses[i]) // speeds[i])
else:
q.append((100-progresses[i]) // speeds[i] + 1)
while q:
v = q.popleft()
lst = list(q)
for i in lst:
if v >= i:
q.popleft()
cnt += 1
else:
break
answer.append(cnt)
cnt = 1
return answer
๋ค๋ฅธ ํ์ด
import math
def solution(progresses, speeds):
progresses = [math.ceil((100 - a) / b) for a, b in zip(progresses, speeds)]
answer = []
front = 0, 0
for idx in range(len(progresses)):
if progresses[idx] > progresses[front]:
answer.append(idx - front)
front = idx
answer.append(len(progresses) - front)
return answer
'Algorithms' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] ์ฒด์ก๋ณต - python (0) | 2023.08.02 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ๋ ๋งต๊ฒ - python (0) | 2023.08.02 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์์ฃผํ์ง ๋ชปํ ์ ์ - python (0) | 2023.08.02 |
[ํ๋ก๊ทธ๋๋จธ์ค] ํ์ผ ๋๋ฒ - python (0) | 2023.08.02 |
[ํ๋ก๊ทธ๋๋จธ์ค] JadenCase ๋ฌธ์์ด ๋ง๋ค๊ธฐ - python (0) | 2023.07.25 |