๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • What would life be If we had no courage to attemp anything?
Problem Solving/BAEKJOON

[baekjoon]python #10828 ์Šคํƒ, #10845 ํ, #10866 ๋ฑ

by DevIseo 2022. 5. 31.

https://www.acmicpc.net/problem/10828

 

10828๋ฒˆ: ์Šคํƒ

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

www.acmicpc.net

n=int(input())
arr = []
for i in range(n):
    arr.append(input().split())

stack = []
for i in range(n):
    if arr[i][0] == 'push':
        stack.append(arr[i][1])

    elif arr[i][0] == 'size':
        print(len(stack))

    elif arr[i][0] == 'empty':
        if len(stack) == 0:
            print(1)
        else:
            print(0)

    elif arr[i][0] == 'pop':
        if len(stack) == 0:
            print(-1)
        else:
            print(stack.pop(len(stack)-1))

    elif arr[i][0] == 'top':
        if len(stack) == 0:
            print(-1)
        else:
            print(stack[-1])

https://www.acmicpc.net/problem/10845

 

10845๋ฒˆ: ํ

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

www.acmicpc.net

n=int(input())
arr = []
for i in range(n):
    arr.append(input().split())

queue = []
for i in range(n):
    if arr[i][0] == 'push':
        queue.append(arr[i][1])

    elif arr[i][0] == 'front':
        if len(queue) == 0:
            print(-1)
        else:
            print(queue[0])

    elif arr[i][0] == 'back':
        if len(queue) == 0:
            print(-1)
        else:
            print(queue[-1])

    elif arr[i][0] == 'size':
        print(len(queue))

    elif arr[i][0] == 'empty':
        if len(queue) == 0:
            print(1)
        else:
            print(0)

    elif arr[i][0] == 'pop':
        if len(queue) == 0:
            print(-1)
        else:
            print(queue.pop(0))

https://www.acmicpc.net/problem/10866

 

10866๋ฒˆ: ๋ฑ

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

www.acmicpc.net

from collections import deque
import sys
n= int(sys.stdin.readline())
arr = []
for i in range(n):
    arr.append(sys.stdin.readline().split())

queue = deque()
for i in range(n):
    if arr[i][0] == 'push_front':
        queue.appendleft(arr[i][1])

    elif arr[i][0] == 'push_back':
        queue.append(arr[i][1])

    elif arr[i][0] == 'pop_front':
        if len(queue) == 0:
            print(-1)
        else:
            print(queue.popleft())

    elif arr[i][0] == 'pop_back':
        if len(queue) == 0:
            print(-1)
        else:
            print(queue.pop())

    elif arr[i][0] == 'size':
        print(len(queue))

    elif arr[i][0] == 'empty':
        if len(queue)==0:
            print(1)
        else:
            print(0)

    elif arr[i][0] == 'front':
        if len(queue)==0:
            print(-1)
        else:
            print(queue[0])

    elif arr[i][0] == 'back':
        if len(queue) == 0:
            print(-1)
        else:
            print(queue[-1])

2022.05.31 - [Problem Solving/ALGORITHM] - deque

 

deque

deque ๋ฐํฌ(deque)์˜ ๊ฐœ๋… - ๋ณดํ†ต์˜ ํ(queue)๋Š” ์„ ์ž…์„ ์ถœ(FIFO)๋กœ ์ž‘๋™ - deque๋Š” ์–‘๋ฐฉํ–ฅํ! - ์•ž, ๋’ค ์–‘์ชฝ ๋ฐฉํ–ฅ์—์„œ element๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์ œ๊ฑฐ ๊ฐ€๋Šฅ - ๋ฐํฌ๋Š” ์–‘ ๋ element์˜ append์™€ pop์ด ์••๋„์ ์œผ๋กœ ๋น ๋ฆ„..

luminous24.tistory.com

 

๋Œ“๊ธ€