Skip to content

Commit 0b2baa2

Browse files
authored
Merge pull request #20 from team-mate-algorithm/jaykxo/week05
Jaykxo/week05
2 parents 58b35af + 69479bb commit 0b2baa2

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

jaykxo/week05/10773_제로.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# 10773 제로
2+
# PyPy3, 메모리: 114580 KB, 시간: 112 ms
3+
# Python3, 메모리: 33192 KB, 시간: 68 ms
4+
5+
import sys
6+
input = sys.stdin.readline
7+
8+
N = int(input())
9+
stack = [] # 숫자를 저장할 스택
10+
11+
for _ in range(N):
12+
num = int(input())
13+
14+
if num != 0: # 0이 아니면 스택에 추가
15+
stack.append(num)
16+
else: # 0이면 마지막 숫자 제거
17+
stack.pop()
18+
19+
print(sum(stack)) # 남은 숫자들의 합 출력

jaykxo/week05/18258_큐_2.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# 18258 큐 2
2+
# PyPy3, 메모리: 225152 KB, 시간: 776 ms
3+
# Python3, 메모리: 136696 KB, 시간: 1388 ms
4+
5+
from collections import deque
6+
import sys
7+
input = sys.stdin.readline
8+
9+
N = int(input())
10+
queue = deque() # 큐 저장용 덱
11+
result = [] # 출력 결과 저장
12+
13+
for _ in range(N):
14+
cmd = input().split()
15+
op = cmd[0] # 명령어 종류 (push, pop, size, empty, front, back)
16+
17+
if op == 'push': # push: 큐에 값 추가
18+
X = int(cmd[1])
19+
queue.append(X)
20+
21+
elif op == 'pop': # pop: 맨 앞 값 제거, 없으면 -1
22+
if not queue:
23+
result.append('-1')
24+
else:
25+
result.append(str(queue.popleft())) # 메서드 한 줄만 써먹음,,
26+
27+
elif op == 'size': # 큐 크기 출력
28+
result.append(str(len(queue)))
29+
30+
elif op == 'empty': # 큐가 비었는지 여부 출력
31+
if not queue:
32+
result.append('1')
33+
else:
34+
result.append('0')
35+
36+
elif op == 'front': # 맨 앞 값 출력, 없으면 -1
37+
if not queue:
38+
result.append('-1')
39+
else:
40+
result.append(str(queue[0]))
41+
42+
elif op == 'back': # 맨 뒤 값 출력, 없으면 -1
43+
if not queue:
44+
result.append('-1')
45+
else:
46+
result.append(str(queue[-1]))
47+
48+
print('\n'.join(result))

jaykxo/week05/2164_카드2.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# 2164 카드2
2+
# PyPy3, 메모리: 117336 KB, 시간: 124 ms
3+
# Python3, 메모리: 51848 KB, 시간: 164 ms
4+
5+
from collections import deque
6+
import sys
7+
input = sys.stdin.readline
8+
9+
N = int(input())
10+
q = deque(range(1, N+1)) # 1부터 N까지 카드 생성
11+
12+
# 카드가 한 장 남을 때까지 반복
13+
while len(q) > 1:
14+
q.popleft() # 맨 위 카드 버림
15+
q.append(q.popleft()) # 다음 카드를 맨 아래로 이동
16+
17+
print(q[0]) # 마지막 남은 카드 출력

jaykxo/week05/28278_스택_2.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# 28278 스택 2
2+
# PyPy3, 메모리: 191116 KB, 시간: 372 ms
3+
# Python3, 메모리: 74472 KB, 시간: 616 ms
4+
5+
import sys
6+
input = sys.stdin.readline
7+
8+
N = int(input())
9+
stack = [] # 스택 저장 리스트
10+
result = [] # 출력 결과 저장
11+
12+
for _ in range(N):
13+
cmd = input().split()
14+
op = cmd[0] # 명령어 번호 (1~5)
15+
16+
if op == '1': # push
17+
x = int(cmd[1])
18+
stack.append(x)
19+
20+
elif op == '2': # pop, 비어있으면 -1
21+
if not stack:
22+
result.append('-1')
23+
else:
24+
result.append(str(stack.pop()))
25+
26+
elif op == '3': # 현재 스택 크기
27+
result.append(str(len(stack)))
28+
29+
elif op == '4': # 비었으면 1, 아니면 0
30+
if not stack:
31+
result.append('1')
32+
else:
33+
result.append('0')
34+
35+
elif op == '5': # 맨 위 원소, 없으면 -1
36+
if not stack:
37+
result.append('-1')
38+
else:
39+
result.append(str(stack[-1]))
40+
41+
print('\n'.join(result))

0 commit comments

Comments
 (0)