Home 백준 - 1874 스택 수열
Post
Cancel

백준 - 1874 스택 수열

문제

1874 스택 수열

screencapture

kotlin code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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")
}
This post is licensed under CC BY 4.0 by the author.

백준 - 5622 다이얼

백준 - 2869 달팽이는 올라가고 싶다