ํ๋?
ํ์ ADT
- isfull: ํ๊ฐ ๊ฝ ์ฐจ์ ๋ ์ด์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ ์ ์๋์ง ์ฌ๋ถ๋ฅผ ํ์ธํ๋ ํจ์
- True: ํ๊ฐ ๊ฝ ์ฐผ์
- False: ํ์ ์ฌ์ ๊ณต๊ฐ์ด ์์
- isempty: ํ๊ฐ ๋น์ด ์๋์ง ํ์ธํ๋ ํจ์
- True: ํ๊ฐ ๋น์์
- False: ํ์ ๋ฐ์ดํฐ๊ฐ ์กด์ฌํจ
- push (enqueue): ํ์ rear(๋ค์ชฝ) ๋์ ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐํ๋ ํจ์
- pop (dequeue): ํ์ front(์์ชฝ) ๋์์ ๋ฐ์ดํฐ๋ฅผ ์ ๊ฑฐํ๋ ํจ์
- front: ํ์ ์์ชฝ ๋์ ์๋ ๋ฐ์ดํฐ๋ฅผ ๋ฐํํ๋ ํจ์(์ ๊ฑฐํ์ง ์์)
- rear: ํ์ ๋ค์ชฝ ๋์ ์๋ ๋ฐ์ดํฐ๋ฅผ ๋ฐํํ๋ ํจ์(์ ๊ฑฐํ์ง ์์)
- data[maxsize]: ํ๊ฐ ๋ด๋ถ์ ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ๋ฐฐ์ด ๋๋ ๋ฆฌ์คํธ์ ์ต๋ ํฌ๊ธฐ maxsize
๋ฆฌ์คํธ๋ฅผ ํ์ฒ๋ผ -> ์ผ์ชฝ์์ ์ฝ์ , ์ญ์ ๊ฐ๋ฅ
queue = []
# ํ์ ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐ
queue.append(1)
queue.append(2)
queue.append(3)
print("Queue after enqueuing:", queue) # [1, 2, 3]
# ํ์์ ๋ฐ์ดํฐ๋ฅผ ์ ๊ฑฐ
first_item = queue.pop(0)
print("Dequeued item:", first_item) # 1
print("Queue after dequeuing:", queue) # [2, 3]
๋ฑ์ ํ์ฒ๋ผ -> ์๋์์ ์ฝ์ , ์ญ์ ๊ฐ๋ฅ
from collections import deque
queue = deque()
# ํ์ ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐ
queue.append(1)
queue.append(2)
queue.append(3)
print("Queue after enqueuing:", queue) # deque([1, 2, 3])
# ํ์์ ๋ฐ์ดํฐ๋ฅผ ์ ๊ฑฐ
first_item = queue.popleft()
print("Dequeued item:", first_item) # 1
print("Queue after dequeuing:", queue) # deque([2, 3])
๋ฌด์๋ณด๋ค ๋ฆฌ์คํธ๋ก ํ๋ฅผ ๊ตฌํํ๋ฉด ๊ฐ์ฅ ์ผ์ชฝ์ ์๋ ๊ฐ์ ๋ฝ์๋ด๋ ๊ฒ์ ์ฐจ์ด๊ฐ ํฌ๋ค.
pop(0)๊ณผ popleft()์ ์ฐ์ฐ ์ฐจ์ด๊ฐ ํฌ๋ฏ๋ก, ํ๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค๋ฉด ๋ฑ์ ๋ฐ๋ก ๋ ์ฌ๋ฆฌ๋๊ฒ ์ฉ์ดํ๋ค๊ณ ํ๋จ๋๋ค.
๋ฌธ์
'''
์์ธํธ์ค ๋ฌธ์
1~n๊น์ง ์๋ฅผ ๋ฃ๊ณ , k๋ฒ์งธ ์๋ฅผ ๋นผ๊ณ , ๊ทธ๋ค์ ์ซ์์์ k๋ฒ์งธ ์๋ฅผ ๋นผ๋๊ฑธ ๋ฐ๋ณตํด์ ๋ง์ง๋ง์ ๋จ๋ ์ return
n=5,k=2, return =3
'''
from collections import deque
def solution(n,k):
dq=deque(range(1,n+1))
while len(dq) != 1:
for _ in range(k-1):
dq.append(dq.popleft())
dq.popleft()
return dq[0]
print(solution(5,2))
๊ธฐ๋ฅ๊ฐ๋ฐ
https://school.programmers.co.kr/learn/courses/30/lessons/42586
'''
progresses=[93, 30, 55]
speeds=[1, 30, 5]
return = [2,1]
'''
from collections import deque
import math
def solution(progresses,speeds):
list=[]
list2=[]
for i in range(len(progresses)):
list.append(100-progresses[i])
for i in range(len(speeds)):
list2.append(math.ceil(list[i]/speeds[i]))
max_day=list2[0]
cnt=0
answer=[]
for i in range(len(progresses)):
if list2[i]<=max_day:
cnt+=1
else:
answer.append(cnt)
cnt=1
max_day=list2[i]
answer.append(cnt)
return answer
print(solution([93, 30, 55],[1, 30, 5]))
์นด๋๋ญ์น
https://school.programmers.co.kr/learn/courses/30/lessons/159994
for๋ฌธ&๋ฆฌ์คํธ
'''
cards1 = ["i", "drink", "water"] , cards2 = ["want", "to"] , gaol = ["i", "want", "to", "drink", "water"] , result ="Yes"
'''
from collections import deque
def solution(cards1, cards2, goal):
for _ in range(len(goal)):
if cards1 and goal[0] == cards1[0]:
cards1.pop(0)
goal.pop(0)
elif cards2 and goal[0] == cards2[0]:
cards2.pop(0)
goal.pop(0)
else:
break
return "Yes" if not goal else "No"
print(solution(["i", "drink", "water"],["want", "to"],["i", "want", "to", "drink", "water"]))
print(solution(["i", "water", "drink"],["want", "to"],["i", "want", "to", "drink", "water"]))
while๋ฌธ&deque
'''
cards1 = ["i", "drink", "water"] , cards2 = ["want", "to"] , gaol = ["i", "want", "to", "drink", "water"] , result ="Yes"
'''
from collections import deque
def solution(cards1, cards2, goal):
cards1 = deque(cards1)
cards2 = deque(cards2)
goal = deque(goal)
while goal:
if cards1 and goal[0] == cards1[0]:
cards1.popleft()
goal.popleft()
elif cards2 and goal[0] == cards2[0]:
cards2.popleft()
goal.popleft()
else:
break
return "Yes" if not goal else "No"
print(solution(["i", "drink", "water"],["want", "to"],["i", "want", "to", "drink", "water"]))
print(solution(["i", "water", "drink"],["want", "to"],["i", "want", "to", "drink", "water"]))
'๐ฏ์๊ณ ๋ฆฌ์ฆ > ์๊ณ ๋ฆฌ์ฆ ์ ๋ฆฌ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํธ๋ฆฌ (0) | 2024.10.04 |
---|---|
ํด์ (3) | 2024.10.03 |
์คํ (0) | 2024.09.10 |
๋ฐฐ์ด (1) | 2024.09.09 |
์ฝํ ์ ๋ฆฌ (0) | 2024.09.09 |