diff --git a/aiden/week_4/boj_10886.py b/aiden/week_4/boj_10886.py new file mode 100644 index 0000000..0fe14da --- /dev/null +++ b/aiden/week_4/boj_10886.py @@ -0,0 +1,31 @@ +from collections import deque +import sys +input=sys.stdin.readline + +def output(command): + cmd = command[0] + if cmd == 'push_front': + mydeq.appendleft(int(command[1])) + elif cmd == 'push_back': + mydeq.append(int(command[1])) + elif cmd == 'pop_front': + return mydeq.popleft() if mydeq else -1 + elif cmd == 'pop_back': + return mydeq.pop() if mydeq else -1 + elif cmd == 'size': + return len(mydeq) + elif cmd == 'empty': + return 1 if not mydeq else 0 + elif cmd == 'front': + return mydeq[0] if mydeq else -1 + elif cmd == 'back': + return mydeq[-1] if mydeq else -1 + +N = int(input()) +mydeq = deque() + +for i in range(N): + command = input().split() + result = output(command) + if result is not None: + print(result) \ No newline at end of file diff --git a/aiden/week_4/boj_11279.py b/aiden/week_4/boj_11279.py new file mode 100644 index 0000000..f9d6f52 --- /dev/null +++ b/aiden/week_4/boj_11279.py @@ -0,0 +1,16 @@ +import heapq +import sys +input = sys.stdin.readline + +N = int(input()) +heap = [] + +for _ in range(N): + x = int(input().strip()) + if x != 0: + heapq.heappush(heap, -x) + else: + if heap: + print(-heapq.heappop(heap)) + else: + print(0) \ No newline at end of file diff --git a/aiden/week_4/boj_15828.py b/aiden/week_4/boj_15828.py new file mode 100644 index 0000000..205482d --- /dev/null +++ b/aiden/week_4/boj_15828.py @@ -0,0 +1,25 @@ +N = int(input()) +packet = [] + +while True: + p = int(input()) + if p == -1: + break + packet.append(p) + +q1 = [] + +for i in packet: + if i == 0: + if q1: # 리스트가 비어있지 않은 경우에만 요소 제거 + q1.pop(0) + else: + if len(q1) < N: # 큐가 가득 차지 않았을 때만 요소 추가 + q1.append(i) + +# 큐에 남아 있는 값 모두 출력하기 +if q1: + print(' '.join(map(str, q1))) +else: + print('empty') + diff --git a/aiden/week_4/boj_1874.py b/aiden/week_4/boj_1874.py new file mode 100644 index 0000000..2e006f5 --- /dev/null +++ b/aiden/week_4/boj_1874.py @@ -0,0 +1,47 @@ +class Stack: + def __init__(self): + self.box = [] + self.output = [] # 출력을 저장하기 위한 리스트 + + def push(self, item): + self.box.append(item) + self.output.append('+') # 리스트에 출력 저장 + + def pop(self): + self.box.pop() + self.output.append('-') # 리스트에 출력 저장 + + def size(self): + return len(self.box) + + def is_empty(self): + return len(self.box) == 0 + + def peek(self): + if not self.is_empty(): + return self.box[-1] + return None + +n = int(input()) +integer = [int(input()) for _ in range(n)] +stack = Stack() + +current = 1 +possible = True # 수열을 만들 수 있는지 여부를 저장 + +for number in integer: + while current <= number: + stack.push(current) + current += 1 + + if stack.peek() == number: + stack.pop() + else: + possible = False + break + +if possible: + for op in stack.output: # 저장된 출력 리스트에서 출력 + print(op) +else: + print("NO") \ No newline at end of file diff --git a/aiden/week_4/boj_2075.py b/aiden/week_4/boj_2075.py new file mode 100644 index 0000000..2d575ae --- /dev/null +++ b/aiden/week_4/boj_2075.py @@ -0,0 +1,20 @@ +import heapq +import sys +input = sys.stdin.readline + +N = int(input()) +heap = [] + +# 처음 N개의 숫자를 힙에 넣음 +for _ in range(N): + numbers = list(map(int, input().split())) + for number in numbers: + if len(heap) < N: + heapq.heappush(heap, number) + else: + # 현재 힙의 루트(최솟값)보다 큰 경우에만 힙에 삽입 + if number > heap[0]: + heapq.heappushpop(heap, number) + +# 마지막에 남은 힙의 루트가 N번째로 큰 값 +print(heap[0]) \ No newline at end of file diff --git a/aiden/week_4/boj_4949.py b/aiden/week_4/boj_4949.py new file mode 100644 index 0000000..8434d14 --- /dev/null +++ b/aiden/week_4/boj_4949.py @@ -0,0 +1,46 @@ +class Stack: + def __init__(self): + self.box = [] + + def push(self, item): + self.box.append(item) + + def pop(self): + return self.box.pop() + + def size(self): + return len(self.box) + + def is_empty(self): + return len(self.box) == 0 + + def peek(self): + if not self.is_empty(): + return self.box[-1] + return None + +def bracket(string): + stack = Stack() + for ch in string: + if ch in '([': + stack.push(ch) + elif ch == ')': + if stack.is_empty() or stack.peek() != '(': + return False + stack.pop() + elif ch == ']': + if stack.is_empty() or stack.peek() != '[': + return False + stack.pop() + return stack.is_empty() + +while True: + string = input().rstrip() + + if string == ".": + break + + if bracket(string): + print("yes") + else: + print("no") \ No newline at end of file diff --git a/aiden/week_4/boj_5430.py b/aiden/week_4/boj_5430.py new file mode 100644 index 0000000..bdd6875 --- /dev/null +++ b/aiden/week_4/boj_5430.py @@ -0,0 +1,28 @@ +def parse_list_strings(arr): + if arr == "[]": + return [] + return list(map(int, arr.strip('[]').split(','))) + +T = int(input()) + +for _ in range(T): + count = 0 + p = input() + n = int(input()) + arr = parse_list_strings(input()) + + for i in p: + if i == 'R': + count += 1 + elif i == 'D': + if not arr: + print('error') + break + if count % 2 == 0: + arr.pop(0) + else: + arr.pop(-1) + else: + if count % 2 != 0: + arr.reverse() + print(f"[{','.join(map(str, arr))}]")