ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ 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
- ์๊ณ ๋ฆฌ์ฆ
- ๋ธ๋ฃจํธํฌ์ค
- MongoDB
- BFS
- Macbook pro 2012 mid 13
- baekjoon
- ์๊ฐ๊ต์ฒด
- Algorithm
- ๋ฐฑ์ค
- ํด์๋งต
- ํ๋ก์ด๋์์ฌ
- dp
- ๋ชฝ๊ณ ๋๋น
- dfs
- ์๋ฐ
- go
- ๋งฅ๋ถ
- Golang
- BOJ
- ํ
- ์ด๋ถํ์
- ๋ถํ ์ ๋ณต
- ๋ฐ๋
- ๋งฅ๋ถํ๋ก
- python3
- ballet
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- AWS
- java
- ์คํ
- Total
- Today
- Yesterday