swift 백준 2493

AppleMango·2024년 7월 5일
let n = Int(readLine()!)!
let input = readLine()!.split(separator: " ").map { Int(String($0))! }

var stack = [(Int, Int)]() // 탑 높이, index
var result = [Int]()

for i in 0..<n {
    while !stack.isEmpty {
        if stack.last!.0 < input[i] {
            stack.removeLast()
        } else {
            result.append(stack.last!.1 + 1)
            break
        }
    }
    if stack.isEmpty {
        result.append(0)
    }
    stack.append((input[i] ,i))
}
print(result.map { String($0) }.joined(separator: " "))

profile
iOS Developer

0개의 댓글