1. λ¬Έμ
- νμ μ΄μ©νμ¬ μ°μ μμ νλ₯Ό ꡬν
2. μ μΆλ ₯
- νμ ꡬνν λ°°μ΄μ ν¬κΈ°λ₯Ό μ λ ₯ λ°μ(μμ μ΄μ§νΈλ¦¬ μ΄κΈ° λλ¬Έμ λ°°μ΄λ‘ ꡬνν μ μμ)
3. λ¬Έμ μ€κ³
- νμ κ²½μ° μμ μ΄μ§νΈλ¦¬ μ΄κΈ° λλ¬Έμ, λ°°μ΄λ‘ ꡬνν μ μμ
- μμ μ΄μ§νΈλ¦¬λ μμ λ Έλκ° 2κ° μ΄ν, μΌμͺ½λΆν° κ°μ΄ μ°¨λ‘λλ‘ μ±μλκ°λ νΈλ¦¬λ₯Ό λ§ν¨
- μ½μ μ°μ°μ κ²½μ° λ°°μ΄μ κ°μ₯ λμ μμλ₯Ό μΆκ°νκ³ , μ°μ μμλ₯Ό λΉκ΅νμ¬ νΈλ¦¬λ₯Ό μ¬κ΅¬μ±ν¨(λ¬Έμ μμλ μ΅μκ°μ΄ μ°μ μμκ° λμ) νΈλ¦¬μ λμ΄λ λ Έλκ° Nκ°μΌ λ, λλ΅ log N μ΄λ―λ‘, μ΅μ μ κ²½μ°(κ°μ₯ μ°μ μμκ° λμ κ°μ΄ μ½μ λμ κ²½μ°, λ¬Έμ μμλ μ΅μκ°) 루νΈλ Έλ κΉμ§ λΉκ΅ν΄μΌ νλ―λ‘ O(log N)μ μκ°λ³΅μ‘λλ₯Ό κ°μ§
- μμ μ°μ°μ κ²½μ° μ°μ μμκ° κ°μ₯ λμ λ£¨νΈ λ Έλλ₯Ό μ κ±°νκ³ κ°μ₯ λ λ¨λ§ λ Έλλ₯Ό λΆλͺ¨ λ Έλμ μμΉ μν€κ³ μ΅μ μ κ²½μ° κ°μ₯ λ¨λ§ λ Έλ κΉμ§ λ΄λ €κ°μΌ νλ―λ‘ νΈλ¦¬μ λμ΄μΈ log N λ§νΌ λ΄λ €κ° μ μμ. λ°λΌμ O(log N)μ μκ°λ³΅μ‘λλ₯Ό κ°μ§
- νμ μ°μ°μ κ²½μ° μ°μ μμκ° κ°μ₯ λμ λ£¨νΈ λ Έλλ₯Ό λ°ννλ©΄ λλ―λ‘, O(1)μ μκ°λ³΅μ‘λλ₯Ό κ°μ§
- λ°°μ΄λ‘ μ°μ μμ νλ₯Ό ꡬννμ λμ λΉκ΅νκ² λλ©΄, μ½μ μ°μ°μ κ²½μ° O(1)κ³Ό O(log N)μΌλ‘ μ°¨μ΄κ° λμ§λ§ μμ μ°μ°μ κ²½μ° O(N)κ³Ό O(log N)μ μ°¨μ΄κ° λ ν¬κΈ° λλ¬Έμ, νμΌλ‘ μ°μ μμ νλ₯Ό ꡬννλ κ²μ΄ λ°λμ§ν¨
4. μ 체 μ½λ
//MARK: - μ°μ μμ ν(ν)
//MARK: - Framework
import Foundation
//MARK: - Type
struct PriorityQueue {
//MARK: - Property
var data: [Int]
var len, capacity: Int
//MARK: - Initializer
init(_ capacity: Int) {
self.capacity = capacity
self.data = Array(repeating: 0, count: self.capacity + 10)
self.len = 1
}
//MARK: - Method
mutating func push(_ x: Int) -> Void {
self.data[self.len] = x
self.len += 1
var currentIndex: Int = self.len - 1
while currentIndex > 1 {
if self.data[currentIndex] < self.data[currentIndex >> 1] {
let temp: Int = self.data[currentIndex]
self.data[currentIndex] = self.data[currentIndex >> 1]
self.data[currentIndex >> 1] = temp
currentIndex >>= 1
} else {
break
}
}
}
mutating func pop() -> Void {
self.data[1] = self.data[self.len - 1]
self.data[self.len - 1] = 0
self.len -= 1
var currentIndex: Int = 1
while true {
var childIndex: Int = -1
if self.len - 1 < (currentIndex << 1) {
break
} else if (0 < (currentIndex << 1) && (currentIndex << 1) <= self.len - 1) && (currentIndex << 1 + 1) > self.len - 1 {
childIndex = currentIndex << 1
} else {
childIndex = (currentIndex << 1) < (currentIndex << 1 + 1) ? (currentIndex << 1) : (currentIndex << 1 + 1)
}
if self.data[currentIndex] > self.data[childIndex] {
let temp: Int = self.data[currentIndex]
self.data[currentIndex] = self.data[childIndex]
self.data[childIndex] = temp
currentIndex = childIndex
} else {
break
}
}
}
func top() -> Int {
return self.data[1]
}
}
//MARK: - Function
func solution() -> Void {
//MARK: - Input
guard let n: Int = Int(readLine() ?? "0") else { return }
var pq: PriorityQueue = PriorityQueue(n)
//MARK: - Process & Output
pq.push(5)
pq.push(8)
pq.push(1)
pq.push(3)
pq.push(3)
print(pq.top()) // 1
pq.pop()
print(pq.top()) // 3
pq.pop()
print(pq.top()) // 3
pq.pop()
print(pq.top()) // 5
pq.pop()
print(pq.top()) // 8
}
solution()
μ 체μ½λλ μ¬κΈ°μμ νμΈν μ μμ΅λλ€.
'Swift Data Structure And Algorithm > Tree' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
νΈλ¦¬μμμ 거리 (0) | 2022.04.07 |
---|---|
νΈλ¦¬μ λμ΄ (0) | 2022.04.07 |
κ³΅ν΅ μ‘°μ μ°ΎκΈ° (0) | 2022.04.07 |
λ°°μ΄μ μ΄μ©ν μ°μ μμ ν(Priority Queue By Array) (0) | 2022.04.06 |
νΈλ¦¬ μν κ²°κ³Ό μΆλ ₯νκΈ° (0) | 2022.04.06 |