• Home
  • About
    • Moon photo

      Moon

      개발자는 자고 싶다.

    • Learn More
    • Twitter
    • Facebook
    • Instagram
    • Github
    • Steam
  • Posts
    • All Posts
    • All Tags
  • Projects

백준 - 1874 스택 수열

07 Jun 2022

Reading time ~1 minute

문제

1874 스택 수열

screencapture

답

kotlin code

fun main() {
    q1874()
}

fun q1874() {
    val size = readln().toInt()
    val inputStack = java.util.Stack<Int>()
    List(size) { readln().toInt() }.reversed().forEach{inputStack.push(it)}
    val resultList = kotlin.collections.ArrayList<Char>()
    val newStack = java.util.Stack<Int>()
    for (i in 1..size) {
        newStack.push(i)
        resultList.add('+')

        while (!newStack.isEmpty() && !inputStack.isEmpty() && newStack.peek() == inputStack.peek()) {
            newStack.pop()
            inputStack.pop()
            resultList.add('-')
        }
    }
    if(newStack.isEmpty() && inputStack.isEmpty())
        resultList.forEach{ println(it) }
    else println("NO")
}


baekjoonkotlin코틀린백준실버지료 구조스택 Share Tweet +1