Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
Tags
- CSS
- react
- ๋ ธ๋ง๋์ฝ๋
- Python
- Til
- ํฌ๋กค๋ง
- KDT
- error
- ๋ชจ๊ฐ์ฝ
- ์ฝ๋ฉ
- ๊ตญ๋น์ง์
- ์ฝ๋ฉ์ ํ
- ํ๋ก๊ทธ๋๋จธ์ค
- ์ฝ๋ฉํ ์คํธ
- ํ๋ก ํธ์๋
- node.js
- mongodb
- ๋ฐฑ์ค
- ํ ์ดํ๋ก์ ํธ
- ๊ทธ๋ฆฌ๋
- ์๊ณ ๋ฆฌ์ฆ
- ํ์ด์ฌ
- JS
- javascript
- HTML
- ํ๋ก์ ํธ
- ๋๋ฆผ์ฝ๋ฉ
- heapq
- ๊ฐ๋ฐ
- fe
Archives
- Today
- Total
๐ฑ → ๐ณ
[๋ฐฑ์ค] 10845๋ฒ : ํ python ๋ณธ๋ฌธ
728x90
https://www.acmicpc.net/problem/10845
๋ฌธ์ ์ ๋ณด
ํ | ์ค๋ฒ4 | 20m | O |
์ค๊ณ ๋ฐฉ๋ฒ
queue class๋ฅผ ์ค๊ณ ํ ์ฝ๋ ์์ฑ
์ฝ๋
import sys
input = sys.stdin.readline
class Queue:
def __init__(self):
self.data = []
def size(self):
return len(self.data)
def empty(self):
if len(self.data) == 0:
return 1
else:
return 0
def push(self, x):
self.data.append(x)
def pop(self):
if len(self.data) == 0:
return -1
else:
# pop(0)์ผ๋ก ํด๋ ๊ฐ์
ans = self.data[0]
self.data = self.data[1:]
return ans
def front(self):
if len(self.data) == 0:
return -1
else:
return self.data[0]
def back(self):
if len(self.data) == 0:
return -1
else:
return self.data[-1]
n = int(input())
queue = Queue()
for _ in range(n):
cmd = input().split()
op = cmd[0]
if op == 'size':
print(queue.size())
elif op == 'empty':
print(queue.empty())
elif op == 'push':
queue.push(cmd[1])
elif op == 'pop':
print(queue.pop())
elif op == 'front':
print(queue.front())
elif op == 'back':
print(queue.back())
else:
print('err')
๋ค๋ฅธํ์ด
์๋ ์ฝ๋์ ๊ฐ์ด class๋ฅผ ๊ตฌํํ์ง ์๊ณ ๋ ํด๊ฒฐํ ์ ์์
from sys import stdin
N = int(stdin.readline())
Que = []
for i in range(N) :
A = stdin.readline().split()
if A[0] == 'push' : Que.append(A[1])
elif A[0] == 'pop' :
if Que : print(Que.pop(0))
else : print(-1)
elif A[0] == 'size' : print(len(Que))
elif A[0] == 'empty' :
if len(Que) == 0 : print(1)
else : print(0)
elif A[0] == 'front' :
if len(Que) == 0 : print(-1)
else : print(Que[0])
elif A[0] == 'back' :
if len(Que) == 0 : print(-1)
else : print(Que[-1])
์๊ฐ ๋ณต์ก๋
$O(n)$
์ด๋ ค์ ๋ ์
X
๋ค๋ง, python์ ์ด์ฉํ๋ ๊ฐ์ฅ ํฐ ์ด์ ๊ฐ ๋ด์ฅํจ์ ๋ฑ์ด ์ ๋์ด์๋ค๋ ๊ฑด๋ฐ ๊ทธ๊ฑธ ์ ํ์ฉํ์ง ๋ชป ํ๊ณ ์๋ค๊ณ ํ๋จ๋์๋ค,. python์ ๋ด์ฅํจ์๋ค์ ์กฐ๊ธ ๋ ์ตํ์ผ ํ ํ์๋ฅผ ๋๊ผ๋ค
์ฐธ๊ณ ์๋ฃ
728x90
'Algorithms' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 10866๋ฒ: ๋ฑ python (0) | 2023.01.08 |
---|---|
[๋ฐฑ์ค] 11047๋ฒ: ๋์ 0 python (0) | 2023.01.08 |
[๋ฐฑ์ค] 1406๋ฒ: ์๋ํฐ python (0) | 2023.01.05 |
[๋ฐฑ์ค] 1874๋ฒ: ์คํ ์์ด python (0) | 2023.01.04 |
[๋ฐฑ์ค] 9012๋ฒ: ๊ดํธ python (0) | 2023.01.03 |