ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ Go
// https://www.acmicpc.net/problem/18258
// ํ์ ๊ฐ๋
์ ์ตํ๊ณ ์ค์ตํ๋ ๋ฌธ์ . ์ฐ์ฐ ๋น ์๊ฐ ๋ณต์ก๋๊ฐ O(1)์ด์ด์ผ ํ๋ค๋ ์ ์ ์ ์.
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var n int
fmt.Fscanln(reader, &n)
var queue []int
for i := 0; i < n; i++ {
var command string
var num int
fmt.Fscanln(reader, &command, &num)
switch command {
case "push":
queue = append(queue, num)
case "pop":
if len(queue) > 1 {
fmt.Fprintln(writer, queue[0])
queue = queue[1:]
} else if len(queue) == 1 {
fmt.Fprintln(writer, queue[0])
queue = []int{}
} else {
fmt.Fprintln(writer, -1)
}
case "size":
fmt.Fprintln(writer, len(queue))
case "empty":
if len(queue) == 0 {
fmt.Fprintln(writer, 1)
} else {
fmt.Fprintln(writer, 0)
}
case "front":
if len(queue) > 0 {
fmt.Fprintln(writer, queue[0])
} else {
fmt.Fprintln(writer, -1)
}
case "back":
if len(queue) > 0 {
fmt.Fprintln(writer, queue[len(queue)-1])
} else {
fmt.Fprintln(writer, -1)
}
}
}
}
๐จ Python3
# https://www.acmicpc.net/problem/18258
# ํ์ ๊ฐ๋
์ ์ตํ๊ณ ์ค์ตํ๋ ๋ฌธ์ . ์ฐ์ฐ ๋น ์๊ฐ ๋ณต์ก๋๊ฐ O(1)์ด์ด์ผ ํ๋ค๋ ์ ์ ์ ์.
"""
deque ํ์ฉ.
Deques support thread-safe, memory efficient appends and pops from either side of the deque
with approximately the same O(1) performance in either direction.
"""
import sys
from collections import deque
if __name__ == "__main__":
n = int(sys.stdin.readline())
dq = deque()
for i in range(n):
inputs = sys.stdin.readline().split()
command = inputs[0]
num = 0
if len(inputs) > 1:
num = inputs[1]
if command == "push":
dq.append(num)
elif command == "pop":
if len(dq) > 0:
print(dq.popleft())
else:
print(-1)
elif command == "size":
print(len(dq))
elif command == "empty":
if len(dq) == 0:
print(1)
else:
print(0)
elif command == "front":
if len(dq) > 0:
print(dq[0])
else:
print(-1)
elif command == "back":
if len(dq) > 0:
print(dq[-1])
else:
print(-1)
"""
# ์๊ฐ์ด๊ณผ ๋ฐ์ํ ์ฝ๋
# (list slicing ์๊ฐ๋ณต์ก๋=O(n+k))
import sys
if __name__ == "__main__":
n = int(sys.stdin.readline())
queue = []
for i in range(n):
inputs = sys.stdin.readline().split()
command = inputs[0]
num = 0
if len(inputs) > 1:
num = inputs[1]
if command == "push":
queue.append(num) # ์๊ฐ๋ณต์ก๋: O(1)
elif command == "pop":
if len(queue) > 1:
print(queue[0])
queue = queue[1:] # ์๊ฐ๋ณต์ก๋: O(n+k)
elif len(queue) == 1:
print(queue[0])
queue = []
else:
print(-1)
elif command == "size":
print(len(queue))
elif command == "empty":
if len(queue) == 0:
print(1)
else:
print(0)
elif command == "front":
if len(queue) > 0:
print(queue[0])
else:
print(-1)
elif command == "back":
if len(queue) > 0:
print(queue[len(queue)-1])
else:
print(-1)
"""
728x90
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 11866๋ฒ / ์์ธํธ์ค ๋ฌธ์ 0 [Go][Python3] (0) | 2020.11.11 |
---|---|
BOJ / 2164๋ฒ / ์นด๋2 [Go][Python3] (0) | 2020.11.10 |
BOJ / 1874๋ฒ / ์คํ ์์ด [Go][Python3] (0) | 2020.11.08 |
BOJ / 4949๋ฒ / ๊ท ํ์กํ ์ธ์ [Go][Python3] (0) | 2020.11.07 |
BOJ / 9012๋ฒ / ๊ดํธ [Go][Python3] (0) | 2020.11.06 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- baekjoon
- AWS
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- Macbook pro 2012 mid 13
- ๋ชฝ๊ณ ๋๋น
- MongoDB
- ๋ฐ๋
- ballet
- dp
- ํด์๋งต
- ๋งฅ๋ถ
- ๋งฅ๋ถํ๋ก
- Algorithm
- Golang
- ํ๋ก์ด๋์์ฌ
- ์๋ฐ
- ์๊ณ ๋ฆฌ์ฆ
- ๋ถํ ์ ๋ณต
- python3
- java
- ํ
- ์ด๋ถํ์
- ๋ฐฑ์ค
- go
- BFS
- ์๊ฐ๊ต์ฒด
- dfs
- ์คํ
- ๋ธ๋ฃจํธํฌ์ค
- BOJ
- Total
- Today
- Yesterday