[Python] 10828번 스택

이세령·2023년 5월 23일
0

알고리즘

목록 보기
10/43

문제

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

풀이과정

  • 시간제한 0.5초
  • 스택 구현하기
  • 클래스로 스택을 구현해주었다.
  • push 명령일 경우 -> 출력 x, 입력만 하기
  • pop 명령일 경우 -> 맨 위 정수 빼고 출력, 없으면 -1 출력
  • size 명령일 경우-> 들어있는 정수의 개수
  • empty 명령일 경우 -> 비어있으면 1, 아니면 0 출력
  • top 명령일 경우 -> 가장 위에 있는 정수 출력, 없으면 -1 출력

옛날에 구현했던 것을 다시 생각해보면서 구현했다.

import sys
n = int(sys.stdin.readline())

class Stack:
    def __init__(self):
        self.stack = []

    def push(self, a):
        self.stack.append(a)

    def pop(self):
        if self.stack:
            x = self.stack[-1]
            self.stack.pop()
            print(x)
        else:
            print(-1)

    def size(self):
        print(len(self.stack))

    def empty(self):
        if self.stack:  # 비어있지 않으면
            print(0)
        else:
            print(1)

    def top(self):
        if self.stack:
            print(self.stack[-1])
        else:
            print(-1)


myStack = Stack()
for _ in range(n):
    command = sys.stdin.readline().split()
    if command[0] == 'push':
        myStack.push(int(command[1]))
    if command[0] == 'pop':
        myStack.pop()
    if command[0] == 'size':
        myStack.size()
    if command[0] == 'empty':
        myStack.empty()
    if command[0] == 'top':
        myStack.top()

메모리 : 31256KB
시간 : 52ms

스택은 어느정도 자신감이 생긴 것 같다!!
어제 이분탐색이 어려웠는데 이분탐색 다시 해보러 가야겠다..

profile
https://github.com/Hediar?tab=repositories

0개의 댓글