From d2254a850fe812a266cb05c98f968c12710eb8ed Mon Sep 17 00:00:00 2001 From: Diger Date: Fri, 20 Oct 2023 18:03:17 +0900 Subject: [PATCH] =?UTF-8?q?=EA=B8=B0=EB=B3=B8=EC=9D=B4=20=EC=A4=91?= =?UTF-8?q?=EC=9A=94=ED=95=98=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _posts/2023-01-01-Algorithm.md | 188 +++++++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 _posts/2023-01-01-Algorithm.md diff --git a/_posts/2023-01-01-Algorithm.md b/_posts/2023-01-01-Algorithm.md new file mode 100644 index 0000000..27b604c --- /dev/null +++ b/_posts/2023-01-01-Algorithm.md @@ -0,0 +1,188 @@ +--- + +title: Algorithm + +date: 2023-01-01 +categories: [Algorithm] +tags: [Algorithm] +layout: post +toc: true +math: true +mermaid: true + +--- + +# DataStructure + +## 괄호 - boj.kr/9012 + +```kotlin +import java.io.BufferedReader +import java.io.BufferedWriter +import java.io.InputStreamReader +import java.io.OutputStreamWriter +import java.util.* + +private val br = BufferedReader(InputStreamReader(System.`in`)) +private val bw = BufferedWriter(OutputStreamWriter(System.out)) + +fun main() { + val t = br.readLine().toInt() + + repeat(t) { + val input = br.readLine() + val stack: Stack = Stack() + + repeat(input.length) { + val current = input.substring(it, it + 1) + + if (current == "(") { + stack.push(current) + } else if (current == ")") { + if (stack.isNotEmpty()) { + if (stack.peek() == "(") { + stack.pop() + } + } else { + stack.push(current) + } + } + } + + if (stack.isEmpty()) { + println("YES") + } else { + println("NO") + } + } +} +``` + +## 균형잡힌 세상 - boj.kr/4949 + +```kotlin +import java.io.BufferedReader +import java.io.BufferedWriter +import java.io.InputStreamReader +import java.io.OutputStreamWriter +import java.util.* + +private val br = BufferedReader(InputStreamReader(System.`in`)) +private val bw = BufferedWriter(OutputStreamWriter(System.out)) + +fun main() { + + while (true) { + val input = br.readLine() + if (input == ".") break + + val stack = Stack() + var isBalanced = true + + for (char in input) { + when (char) { + '(', '[' -> stack.push(char) + ')' -> { + if (stack.isEmpty() || stack.pop() != '(') { + isBalanced = false + break + } + } + + ']' -> { + if (stack.isEmpty() || stack.pop() != '[') { + isBalanced = false + break + } + } + } + } + + if (isBalanced && stack.isEmpty()) { + println("yes") + } else { + println("no") + } + } +} +``` + +## 덱 - boj.kr/10866 + +```kotlin +import java.io.BufferedReader +import java.io.BufferedWriter +import java.io.InputStreamReader +import java.io.OutputStreamWriter + +private val br = BufferedReader(InputStreamReader(System.`in`)) +private val bw = BufferedWriter(OutputStreamWriter(System.out)) + +fun main() { + val n = br.readLine().toInt() + val deque = ArrayDeque() + + repeat(n) { + val input = br.readLine().split(" ") + when (input[0]) { + "push_back" -> deque.addLast(input[1].toInt()) + "pop_back" -> bw.write(if (deque.isEmpty()) "-1\n" else "${deque.removeLast()}\n") + "push_front" -> deque.addFirst(input[1].toInt()) + "pop_front" -> bw.write(if (deque.isEmpty()) "-1\n" else "${deque.removeFirst()}\n") + "front" -> bw.write(if (deque.isEmpty()) "-1\n" else "${deque.first()}\n") + "back" -> bw.write(if (deque.isEmpty()) "-1\n" else "${deque.last()}\n") + "size" -> bw.write("${deque.size}\n") + "empty" -> bw.write(if (deque.isEmpty()) "1\n" else "0\n") + } + } + br.close() + bw.close() +} +``` + +## 수찾기 - boj.kr/1920 + +```kotlin +import java.io.BufferedReader +import java.io.BufferedWriter +import java.io.InputStreamReader +import java.io.OutputStreamWriter + +private val br = BufferedReader(InputStreamReader(System.`in`)) +private val bw = BufferedWriter(OutputStreamWriter(System.out)) + + +fun main() { + val n = br.readLine().toInt() + val array = br.readLine().split(" ").map { it.toInt() }.toHashSet() + + val m = br.readLine().toInt() + val target = br.readLine().split(" ").map { it.toInt() } + + for (value in target) { + if (value in array) { + println(1) + } else { + println(0) + } + } +} +``` + +# String + +# BruteForce + +# Implementation + +# Greedy + +# Back-Tracking + +# Graph + +# Dijkstra + +# Divide And Conquer + +# DP