Skip to content

Commit 510c230

Browse files
committed
Queue/Stack Python Code
1 parent c37068e commit 510c230

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
문제: 제로 (http://boj.kr/10773)
3+
Tier: Silver 4 (2021.01.14 기준)
4+
Comment: 0이 나올때 가장 최근의 값을 뺍니다.
5+
즉, FIFO 구조를 띄고 있고, 자연스럽게 스택을 사용하시면 됩니다.
6+
"""
7+
8+
import sys
9+
10+
N = int(sys.stdin.readline().rstrip())
11+
_list = []
12+
for i in range(N):
13+
tmp = int(sys.stdin.readline().rstrip())
14+
if tmp == 0:
15+
_list.pop()
16+
else:
17+
_list.append(tmp)
18+
19+
print(sum(_list))
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
문제: 요세푸스 문제 0 (http://boj.kr/1158)
3+
Tier: Silver 5 (2021.01.12 기준)
4+
Comment: 큐를 활용할 수 있는 대표적인 문제입니다.
5+
출력이 조금 특이하기 때문에 출력만 유의해서 풀어주세요.
6+
"""
7+
8+
from collections import deque
9+
10+
N, K = map(int, input().split())
11+
_list = []
12+
q = deque([i + 1 for i in range(N)])
13+
14+
while len(q) != 0:
15+
q.rotate(-K)
16+
_list.append(q.pop())
17+
18+
print('<' + ', '.join(map(str, _list)) + '>')
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
문제: 카드 2 (http://boj.kr/2164)
3+
Tier: Silver 4 (2021.01.12 기준)
4+
Comment: 큐를 배웠으니 큐를 통해서 풀이 하였습니다.
5+
한번은 그냥 빼고, 한번은 뺀 걸 다시 넣는데, 결국 두 연산을 한 번씩 하면 크기가 1씩 감소합니다.
6+
즉 크기가 1이 될 때 까지 빼고, 빼서 넣고를 반복하면 쉽게 해결할 수 있겠네요.
7+
물론 큐를 사용하지 않고 수식으로도 풀 수 있습니다.
8+
어떻게 할 수 있을까요?
9+
"""
10+
11+
from collections import deque
12+
13+
li = deque([i for i in range(1, int(input()) + 1)])
14+
15+
while len(li) >= 2:
16+
li.popleft()
17+
li.rotate(-1)
18+
19+
print(li[0])
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
문제: 괄호 (http://boj.kr/9012)
3+
Tier: Silver 4 (2021.01.14 기준)
4+
Comment: 스택 하면 떠올릴 수 있는 가장 유명한 문제입니다.
5+
그런데, 어차피 괄호 종류는 하나인데 굳이 스택을 써야 할까요?
6+
(백준 pypy3 기준 효율성 1위 코드입니다 ㅎㅎ;;)
7+
"""
8+
9+
import sys
10+
11+
N = int(sys.stdin.readline().rstrip())
12+
result = ""
13+
14+
for i in range(N):
15+
testcase = sys.stdin.readline().rstrip()
16+
cnt = 0
17+
for c in testcase:
18+
cnt += 1 if c == '(' else -1
19+
if cnt < 0:
20+
result += "NO\n"
21+
break
22+
else:
23+
result += "YES\n" if cnt == 0 else "NO\n"
24+
25+
print(result)

0 commit comments

Comments
 (0)