๋ฐ์ํ
import Foundation
func solution(_ numbers: [Int]) -> [Int] {
var stack = [Int]()
var res = Array(repeating: -1, count: numbers.count)
for i in 0..<numbers.count {
while !stack.isEmpty && numbers[stack.last!] < numbers[i] {
res[stack.removeLast()] = numbers[i]
}
stack.append(i)
}
return res
}
O(N)์ ์๊ฐ๋ณต์ก๋๋ก ํ์ด์ผ ์๊ฐ์ด๊ณผ๊ฐ ๋์ง ์๋๋ค.
์ํ๋ฅผ ํ๋ฒ๋ง ํ๊ธฐ ์ํด stack์ ์ด์ฉํ๋ค.
๋ฐ์ํ
'๐ ์ฝํ > ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] ํ๋ก๊ทธ๋๋จธ์ค12911 ๋ค์ ํฐ ์ซ์ (0) | 2023.05.13 |
---|---|
[Swift] ํ๋ก๊ทธ๋๋จธ์ค42862 ์ฒด์ก๋ณต (0) | 2023.05.13 |
[Swift] ํ๋ก๊ทธ๋๋จธ์ค181188 ์๊ฒฉ์์คํ (0) | 2023.05.12 |
[Swift] ํ๋ก๊ทธ๋๋จธ์ค92342 ์๊ถ๋ํ (0) | 2023.05.11 |
[Swift] ํ๋ก๊ทธ๋๋จธ์ค118667 ๋ ํ ํฉ ๊ฐ๊ฒ ๋ง๋ค๊ธฐ (0) | 2023.05.11 |