목록Algorithm (38)
선릉역 1번 출구
from collections import deque import sys deq = deque() for _ in range(int(sys.stdin.readline().strip())): cmd = list(sys.stdin.readline().rstrip().split()) if cmd[0] == "push": deq.append(cmd[1]) elif cmd[0] == "pop": if deq: print(deq.popleft()) else: print(-1) elif cmd[0] == "size": print(len(deq)) elif cmd[0] == "empty": if deq: print(0) else: print(1) elif cmd[0] == "front": if deq: print(de..
python에서 queue 사용하는 방법 *queue는 FIFO의 구조를 가지고 있다. (선입 선출) 1. list 사용하기 list에서 push연산은 append를 사용해 구현할 수 있고, pop연산은 pop(0)을 사용해 구현할 수 있다. 그러나 pop연산의 시간복잡도는 O(N)이어서 큐에 적합한 자료구조라고는 볼 수 없다. 2. collections module의 deque 사용하기 deque는 double-ended queue로 양방향 큐이다. 앞, 뒤 양쪽 방향에서 데이터를 추가하거나 제거가 가능한 큐이다. from collections import deque deq = deque() # 데이터 push deq.append() //데크의 오른쪽 끝에 데이터 push deq.appendleft()..
from sys import stdin word = list(stdin.readline().strip()) word2 = [] num = int(stdin.readline()) for i in range(num): command = stdin.readline().strip() if command[0] == "P": word.append(command[2]) elif command[0] == "B": if word: word.pop() else: continue elif command[0] == "D": if word2: word.append(word2.pop()) else: continue elif command[0] == 'L': if word: word2.append(word.pop()) print(..
num = int(input()) stack = [] ls = [] k = 1 flag = 0 for _ in range(num): a = int(input()) while k
def push(vps): stack.append(vps) def pop(): if stack: stack.pop() return 0 return -1 for _ in range(int(input())): stack = [] a = input() k = 0 for i in range(len(a)): if a[i] == '(': push('(') elif a[i] == ')': k += pop() if k != 0 or stack: print("NO") else: print("YES") 괄호를 열고 닫는 개수가 일치해야 이 문제가 풀린다. 그러나 주의할 점은 ()의 순서여야 한다는 것이다. )(는 안됨 그래서 생각한 것은 1. ()의 순서대로 (의 개수와 )의 개수가 일치할 때 => YES 출력 2. (의..