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

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

 

10866๋ฒˆ: ๋ฑ

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

www.acmicpc.net


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

๐ŸŽจ Go

// https://www.acmicpc.net/problem/10866
// ๋ฑ์˜ ๊ฐœ๋…์„ ์ตํžˆ๊ณ  ์‹ค์Šตํ•˜๋Š” ๋ฌธ์ œ
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 deque []int
	for i := 0; i < n; i++ {
		var command string
		var num int
		fmt.Fscanln(reader, &command, &num)

		if command == "push_back" {
			deque = pushBack(deque, num)
		} else if command == "push_front" {
			deque = pushFront(deque, num)
		} else if command == "pop_front" {
			var front int
			deque, front = popFront(deque)
			fmt.Fprintln(writer, front)
		} else if command == "pop_back" {
			var back int
			deque, back = popBack(deque)
			fmt.Fprintln(writer, back)
		} else if command == "size" {
			fmt.Fprintln(writer, size(deque))
		} else if command == "empty" {
			if isEmpty(deque) {
				fmt.Fprintln(writer, 1)
			} else {
				fmt.Fprintln(writer, 0)
			}
		} else if command == "front" {
			fmt.Fprintln(writer, front(deque))
		} else if command == "back" {
			fmt.Fprintln(writer, back(deque))
		}
	}
}

func pushBack(deque []int, num int) []int {
	deque = append(deque, num)
	return deque
}

func pushFront(deque []int, num int) []int {
	deque = append([]int{num}, deque...)
	return deque
}

func popFront(deque []int) ([]int, int) {
	if isEmpty(deque) {
		return deque, -1
	}
	front := deque[0]
	deque = deque[1:]
	return deque, front
}

func popBack(deque []int) ([]int, int) {
	if isEmpty(deque) {
		return deque, -1
	}
	back := deque[len(deque)-1]
	deque = deque[:len(deque)-1]
	return deque, back
}

func size(deque []int) int {
	return len(deque)
}

func isEmpty(deque []int) bool {
	if size(deque) == 0 {
		return true
	}
	return false
}

func front(deque []int) int {
	if isEmpty(deque) {
		return -1
	}
	return deque[0]
}

func back(deque []int) int {
	if isEmpty(deque) {
		return -1
	}
	return deque[len(deque)-1]
}

๐ŸŽจ Python3

# https://www.acmicpc.net/problem/10866
# ๋ฑ์˜ ๊ฐœ๋…์„ ์ตํžˆ๊ณ  ์‹ค์Šตํ•˜๋Š” ๋ฌธ์ œ
import sys

class Deque:
    def __init__(self):
        self.list = []
    
    def push_front(self, x):
        self.list.insert(0, x)
    
    def push_back(self, x):
        self.list.append(x)

    def pop_front(self):
        if self.is_empty():
            return -1
        return self.list.pop(0)

    def pop_back(self):
        if self.is_empty():
            return -1
        return self.list.pop(-1)

    def size(self):
        return len(self.list)

    def is_empty(self):
        if self.size() == 0:
            return True
        return False

    def front(self):
        if self.size() == 0:
            return -1
        return self.list[0]

    def back(self):
        if self.size() == 0:
            return -1
        return self.list[-1]

if __name__ == "__main__":
    n = int(sys.stdin.readline())
    deque = Deque()
    for i in range(n):
        inputs = sys.stdin.readline().split()
        command = inputs[0]
        if command == "push_back":
            deque.push_back(inputs[1])
        elif command == "push_front":
            deque.push_front(inputs[1])
        elif command == "front":
            print(deque.front())
        elif command == "back":
            print(deque.back())
        elif command == "size":
            print(deque.size())
        elif command == "empty":
            if deque.is_empty():
                print(1)
            else:
                print(0)
        elif command == "pop_front":
            print(deque.pop_front())
        elif command == "pop_back":
            print(deque.pop_back())
728x90
๋Œ“๊ธ€