[์ค์ํํธ ์๊ณ ๋ฆฌ์ฆ] ํ๋ก๊ทธ๋๋จธ์ค: ๋ฐฐ๋ฌ
๋ฌธ์
N๊ฐ์ ๋ง์๋ก ์ด๋ฃจ์ด์ง ๋๋ผ๊ฐ ์์ต๋๋ค. ์ด ๋๋ผ์ ๊ฐ ๋ง์์๋ 1๋ถํฐ N๊น์ง์ ๋ฒํธ๊ฐ ๊ฐ๊ฐ ํ๋์ฉ ๋ถ์ฌ๋์ด ์์ต๋๋ค. ๊ฐ ๋ง์์ ์๋ฐฉํฅ์ผ๋ก ํตํํ ์ ์๋ ๋๋ก๋ก ์ฐ๊ฒฐ๋์ด ์๋๋ฐ, ์๋ก ๋ค๋ฅธ ๋ง์ ๊ฐ์ ์ด๋ํ ๋๋ ์ด ๋๋ก๋ฅผ ์ง๋์ผ ํฉ๋๋ค. ๋๋ก๋ฅผ ์ง๋ ๋ ๊ฑธ๋ฆฌ๋ ์๊ฐ์ ๋๋ก๋ณ๋ก ๋ค๋ฆ ๋๋ค. ํ์ฌ 1๋ฒ ๋ง์์ ์๋ ์์์ ์์ ๊ฐ ๋ง์๋ก ์์ ๋ฐฐ๋ฌ์ ํ๋ ค๊ณ ํฉ๋๋ค. ๊ฐ ๋ง์๋ก๋ถํฐ ์์ ์ฃผ๋ฌธ์ ๋ฐ์ผ๋ ค๊ณ ํ๋๋ฐ, N๊ฐ์ ๋ง์ ์ค์์ K ์๊ฐ ์ดํ๋ก ๋ฐฐ๋ฌ์ด ๊ฐ๋ฅํ ๋ง์์์๋ง ์ฃผ๋ฌธ์ ๋ฐ์ผ๋ ค๊ณ ํฉ๋๋ค. ๋ค์์ N = 5, K = 3์ธ ๊ฒฝ์ฐ์ ์์์ ๋๋ค.

์ ๊ทธ๋ฆผ์์ 1๋ฒ ๋ง์์ ์๋ ์์์ ์ [1, 2, 4, 5] ๋ฒ ๋ง์๊น์ง๋ 3 ์ดํ์ ์๊ฐ์ ๋ฐฐ๋ฌํ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ 3๋ฒ ๋ง์๊น์ง๋ 3์๊ฐ ์ด๋ด๋ก ๋ฐฐ๋ฌํ ์ ์๋ ๊ฒฝ๋ก๊ฐ ์์ผ๋ฏ๋ก 3๋ฒ ๋ง์์์๋ ์ฃผ๋ฌธ์ ๋ฐ์ง ์์ต๋๋ค. ๋ฐ๋ผ์ 1๋ฒ ๋ง์์ ์๋ ์์์ ์ด ๋ฐฐ๋ฌ ์ฃผ๋ฌธ์ ๋ฐ์ ์ ์๋ ๋ง์์ 4๊ฐ๊ฐ ๋ฉ๋๋ค.
๋ง์์ ๊ฐ์ N, ๊ฐ ๋ง์์ ์ฐ๊ฒฐํ๋ ๋๋ก์ ์ ๋ณด road, ์์ ๋ฐฐ๋ฌ์ด ๊ฐ๋ฅํ ์๊ฐ K๊ฐ ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง ๋, ์์ ์ฃผ๋ฌธ์ ๋ฐ์ ์ ์๋ ๋ง์์ ๊ฐ์๋ฅผ return ํ๋๋ก solution ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์.
์ ํ์ฌํญ
- ๋ง์์ ๊ฐ์ N์ 1 ์ด์ 50 ์ดํ์ ์์ฐ์์ ๋๋ค.
- road์ ๊ธธ์ด(๋๋ก ์ ๋ณด์ ๊ฐ์)๋ 1 ์ด์ 2,000 ์ดํ์ ๋๋ค.
- road์ ๊ฐ ์์๋ ๋ง์์ ์ฐ๊ฒฐํ๊ณ ์๋ ๊ฐ ๋๋ก์ ์ ๋ณด๋ฅผ ๋ํ๋ ๋๋ค.
- road๋ ๊ธธ์ด๊ฐ 3์ธ ๋ฐฐ์ด์ด๋ฉฐ, ์์๋๋ก (a, b, c)๋ฅผ ๋ํ๋
๋๋ค.
- a, b(1 โค a, b โค N, a != b)๋ ๋๋ก๊ฐ ์ฐ๊ฒฐํ๋ ๋ ๋ง์์ ๋ฒํธ์ด๋ฉฐ, c(1 โค c โค 10,000, c๋ ์์ฐ์)๋ ๋๋ก๋ฅผ ์ง๋๋๋ฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ์ ๋๋ค.
- ๋ ๋ง์ a, b๋ฅผ ์ฐ๊ฒฐํ๋ ๋๋ก๋ ์ฌ๋ฌ ๊ฐ๊ฐ ์์ ์ ์์ต๋๋ค.
- ํ ๋๋ก์ ์ ๋ณด๊ฐ ์ฌ๋ฌ ๋ฒ ์ค๋ณตํด์ ์ฃผ์ด์ง์ง ์์ต๋๋ค.
- K๋ ์์ ๋ฐฐ๋ฌ์ด ๊ฐ๋ฅํ ์๊ฐ์ ๋ํ๋ด๋ฉฐ, 1 ์ด์ 500,000 ์ดํ์ ๋๋ค.
- ์์์ ๋ ๋ง์๊ฐ์ ํญ์ ์ด๋ ๊ฐ๋ฅํ ๊ฒฝ๋ก๊ฐ ์กด์ฌํฉ๋๋ค.
- 1๋ฒ ๋ง์์ ์๋ ์์์ ์ด K ์ดํ์ ์๊ฐ์ ๋ฐฐ๋ฌ์ด ๊ฐ๋ฅํ ๋ง์์ ๊ฐ์๋ฅผ return ํ๋ฉด ๋ฉ๋๋ค.
ํ์ด
์ ํ์ ์ธ ๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ์ต๋จ๊ฑฐ๋ฆฌ๋ฅผ ์ฐพ๋ ๋ฌธ์ ์์ต๋๋ค. C++ ๊ณผ๋ ๋ค๋ฅด๊ฒ ์ค์ํํธ์์๋ ํ๊ณผ ์ฐ์ ์์ํ ์๋ฃ๊ตฌ์กฐ๊ฐ ์์ด์ ์๋ฃ๊ตฌ์กฐ๋ฅผ ์์ ํ ์๋ก ๋ง๋ค์ด์ ์ฌ์ฉํ๊ฑฐ๋, ๋ฐฐ์ด์ ์์๋ค์ ์ง์ด๋ฃ๊ณ ๋นผ๋ด๊ธฐ ์ง์ ์ ์ฐ์ ์์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ์ ํ๊ณ ์์๋ฅผ ๋นผ๋ ๋ฐฉ๋ฒ์ผ๋ก ํด๊ฒฐํ ์ ์์ต๋๋ค. ๋ ๋ฐฉ๋ฒ์ ๋ํ ์ฝ๋๋ฅผ ๋ชจ๋ ์ฒจ๋ถํฉ๋๋ค.
์ฝ๋
ํ๊ณผ ์ฐ์ ์์ ํ๋ฅผ ์ ์ํ์ง ์๊ณ ์ ๋ ฌ์ ํตํด ํด๊ฒฐํ ํ์ด
import Foundation func calcDists(_ distances: inout [Int], _ costs: inout [[(Int, Int)]]) { var pq : [(Int, Int)] = [] pq.append((1, 0)) distances[1] = 0 while(!pq.isEmpty) { pq.sort(by: <) let (here, costHere) = pq.removeFirst() for (there, costThere) in costs[here] { if distances[there] > costHere + costThere { distances[there] = costHere + costThere pq.append((there, distances[there])) } } } } func solution(_ N:Int, _ road:[[Int]], _ k:Int) -> Int { var distances = Array(repeating: 987654321, count: N + 1) var costs = Array(repeating: [(Int, Int)](), count: N + 1) for info in road { costs[info[0]].append((info[1], info[2])) costs[info[1]].append((info[0], info[2])) } calcDists(&distances, &costs) return distances.filter({ $0 <= k }).count }
ํ๊ณผ ์ฐ์ ์์ ํ๋ฅผ ๊ตฌํํ ํ์ด
import Foundation struct Heap<Element> { var elements : [Element] let sort : (Element, Element) -> Bool var isEmpty : Bool { return elements.isEmpty } var count : Int { return elements.count } init(elements: [Element] = [], sort: @escaping (Element, Element) -> Bool) { self.elements = elements self.sort = sort buildHeap() } // ๋ง์ง๋ง ๋ฆฌํ๋
ธ๋์ ๋ถ๋ชจ๋
ธ๋๋ถํฐ ๋ฃจํธ๋
ธ๋๊น์ง diveDown์ ์ํํ๋ค. mutating func buildHeap() { for index in (0..<(count / 2)).reversed() { diveDown(from: index) } } func peek() -> Element? { return elements.first } func isRoot(_ index: Int) -> Bool { return index == 0 } func leftChildIndex(of index: Int) -> Int { return (2 * index) + 1 } func rightChildIndex(of index: Int) -> Int { return (2 * index) + 2 } func parentIndex(of index: Int) -> Int { return (index - 1) / 2 } func isWithHigherPriority (at firstIdx: Int, than secondIdx: Int) -> Bool { return sort(elements[firstIdx], elements[secondIdx]) } // ์ผ์ชฝ ์๋
๋
ธ๋๊ฐ ์๋ ๊ฒฝ์ฐ์๋ ๋ถ๋ชจ๋
ธ๋์ ๋น๊ตํ์ฌ ๋ ํฐ ๋
ธ๋์ ์ธ๋ณ์ค๋ฅผ ๋ฐํํ๊ณ , ์๋ค๋ฉด ๋ถ๋ชจ๋
ธ๋์ ์ธ๋ฑ์ค๋ฅด ๋ฐํํ๋ค. func higherPriorityWithChild(parentIndex: Int, childIndex: Int) -> Int { guard childIndex < count && isWithHigherPriority(at: childIndex, than: parentIndex) else { return parentIndex } return childIndex } // ์ผ์ชฝ ์๋
๋
ธ๋์ ๋ถ๋ชจ๋
ธ๋ ์ค ์ฐ์ ์์๊ฐ ๋ ํฐ ๋
ธ๋์ ์ค๋ฅธ์ชฝ ์๋
๋
ธ๋๋ฅผ ๋น๊ตํ์ฌ ๋ ํฐ ๋
ธ๋์ ์ธ๋ฑ์ค๋ฅผ ๋ฐํํ๋ค. func higherPriorityOfChildren(for parent: Int) -> Int { return higherPriorityWithChild( parentIndex: higherPriorityWithChild(parentIndex: parent, childIndex: leftChildIndex(of: parent)), childIndex: rightChildIndex(of: parent)) } mutating func swapElement (at firstIndex: Int, with secondIndex: Int){ elements.swapAt(firstIndex, secondIndex) } // ํธ๋ฆฌ์ ๊ฐ์ฅ ๋ง์ง๋ง์ ์๋ก์ด ๋
ธ๋๋ฅผ ์ฝ์
ํ๊ณ swimUp์ผ๋ก ํธ๋ฆฌ๋ฅผ ์ฌ์กฐ์ ํ๋ค. mutating func push (_ element: Element) { elements.append(element) swimUp(fromIndex: count - 1) } // ์ง์ ๋ ์ธ๋ฑ์ค๋ถํฐ ์์ ์ ๋ถ๋ชจ๋
ธ๋์ ๋น๊ตํ๋ฉด์ ๋ถ๋ชจ๋
ธ๋๋ณด๋ค ์ฐ์ ์์๊ฐ ๋๋ค๋ฉด ๋ ๋
ธ๋๋ฅผ ๊ต์ฒดํ ๋ค ์๋กญ๊ฒ ๋ถ๋ชจ๋
ธ๋๊ฐ ๋ ๋
ธ๋๋ถํฐ swimUp์ ๋ค์ ์ํํ๋ค. mutating func swimUp(fromIndex index: Int) { let parentIdx = parentIndex(of: index) guard !isRoot(index), isWithHigherPriority(at: index, than: parentIdx) else { return } swapElement(at: index, with: parentIdx) swimUp(fromIndex: parentIdx) } // ๋ฃจํธ๋
ธ๋์ ๋ด์ฉ์ ๋ง์ง๋ง ๋ฆฌํ๋
ธ๋์ ๋ด์ฉ๊ณผ ๊ตํํ๋ค ๋ง์ง๋ง ๋ฆฌํ๋
ธ๋๋ฅผ ๋ผ์ด๋ฒ๋ฆฐ๋ค. ๊ทธ๋ฆฌ๊ณ ๋ฃจํธ๋
ธ๋๋ถํฐ diveDown์ ์ํํ์ฌ ํธ๋ฆฌ๋ฅผ ์ฌ์กฐ์ ํ๋ค. mutating func pop() -> Element? { guard !isEmpty else { return nil } swapElement(at: 0, with: count - 1) let removedElement = elements.removeLast() if !isEmpty { diveDown(from: 0) } return removedElement } // ๋ถ๋ชจ๋
ธ๋์ ์๋
๋
ธ๋๋ฅผ ๋น๊ตํ๋ฉด์ ์๋
๋
ธ๋์ ์ฐ์ ์์๊ฐ ๋ ๋๋ค๋ฉด ๊ตํํ๋ค. mutating func diveDown(from index: Int){ let childIndex = higherPriorityOfChildren(for: index) if index == childIndex { return } swapElement(at: index, with: childIndex) diveDown(from: childIndex) } } struct PriorityQueue<T> { var heap: Heap<T> init(_ elements: [T], _ sort: @escaping (T, T) -> Bool) { heap = Heap(elements: elements, sort: sort) } var count : Int { return heap.count } var isEmpty : Bool { return heap.isEmpty } mutating func clear () { heap = Heap<T>(elements: [], sort: heap.sort) } func top () -> T? { return heap.peek() } mutating func pop() -> T? { return heap.pop() } mutating func push(_ element: T) { heap.push(element) } } func calcDists(_ distances: inout [Int], _ costs: inout [[(Int, Int)]]) { var pq = PriorityQueue<(Int, Int)>([], { $0.1 < $1.1 }) pq.push((1, 0)) distances[1] = 0 while(!pq.isEmpty) { let (here, costHere) = pq.pop()! for (there, costThere) in costs[here] { if distances[there] > costHere + costThere { distances[there] = costHere + costThere pq.push((there, distances[there])) } } } } func solution(_ N:Int, _ road:[[Int]], _ k:Int) -> Int { var distances = Array(repeating: 987654321, count: N + 1) var costs = Array(repeating: [(Int, Int)](), count: N + 1) for info in road { costs[info[0]].append((info[1], info[2])) costs[info[1]].append((info[0], info[2])) } calcDists(&distances, &costs) return distances.filter({ $0 <= k }).count }
'๐๐ปโโ๏ธ ํผ-์์ค > ๐ฃ ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋๊ธ
์ด ๊ธ ๊ณต์ ํ๊ธฐ
-
๊ตฌ๋
ํ๊ธฐ
๊ตฌ๋ ํ๊ธฐ
-
์นด์นด์คํก
์นด์นด์คํก
-
๋ผ์ธ
๋ผ์ธ
-
ํธ์ํฐ
ํธ์ํฐ
-
Facebook
Facebook
-
์นด์นด์ค์คํ ๋ฆฌ
์นด์นด์ค์คํ ๋ฆฌ
-
๋ฐด๋
๋ฐด๋
-
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
-
Pocket
Pocket
-
Evernote
Evernote
๋๊ธ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.