ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ

 

18258๋ฒˆ: ํ 2

์ฒซ์งธ ์ค„์— ์ฃผ์–ด์ง€๋Š” ๋ช…๋ น์˜ ์ˆ˜ N (1 ≤ N ≤ 2,000,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๋ช…๋ น์ด ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง€๋Š” ์ •์ˆ˜๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค. ๋ฌธ์ œ์— ๋‚˜์™€์žˆ์ง€

www.acmicpc.net


โœ๐Ÿป ํ’€์ด

๐ŸŽจ 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
๋Œ“๊ธ€