반응형


문제

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


입출력부분에서 시간초과가 떴다.

http://itnovice1.blogspot.com/2019/03/python-input-vs-sysstdinreadlines.html

유의 : 일반 IDE에서는 실행이 되지 않는다. 때문에 jupyter, spyder 등에서 실행되지 

않는다.

jupyter만 쓰다가 pycharm을 설치하여 사용하였다.


pycham 설치 참조

https://wikidocs.net/20343 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import sys
 
 
class Stack:
    def __init__(self):
        self.len = 0
        self.list = []
 
    def push(self, num):
        self.list.append(num)
        self.len += 1
 
    def pop(self):
        if self.len == 0:  # 스택이 비어있다면
                return -1
        pop_result = self.list[self.len - 1]  # 스택이 비어있지 않다면 마지막 인자 담기
            del self.list[self.len - 1]  # 마지막인자 삭제
            self.len -= 1  # 길이 조정
            return pop_result
 
    def size(self):
        return self.len  # 길이반환
 
      def empty(self):
        return 1 if self.len == 0 else 0  # 비어있으면 1 아니면 0
 
 
    def top(self):
        return -1 if self.len == 0 else self.list[self.len - 1]  # 비어있으면 -1 아니면 마지막인자 return
 
 
num = int(input())
stack = Stack()
while (num > 0):  # num만큼 반복
      num -= 1
    # input_command=input().split() #명령 받아서 배열에 담음 시간초과의 원인
      # command=input_command[0]
    command = sys.stdin.readline().split()
 
    if command[0== "push":
        stack.push(command[1])
    elif command[0== "pop":
        print(stack.pop())
    elif command[0== "size":
        print(stack.size())
    elif command[0== "empty":
        print(stack.empty())
    elif command[0== "top":
        print(stack.top())
    else:
        print("end")

cs


반응형

'알고리즘(python) > 자료구조' 카테고리의 다른 글

[Python]Queue 백준 18258  (0) 2020.01.15
[Python]Stack 백준 1874  (0) 2019.12.15
[Python]Stack 백준 4949  (0) 2019.12.14
[Python]Stack 백준 9012  (0) 2019.12.14
[Python]Stack 백준 10773  (0) 2019.12.14
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기