diff --git "a/snughnu/2\354\243\274\354\260\250_\354\236\254\352\267\200_\353\260\261\355\212\270\353\236\230\355\202\271/Programmers_42883.swift" "b/snughnu/5\354\243\274\354\260\250_\352\267\270\353\246\254\353\224\224/Programmers_42883.swift" similarity index 100% rename from "snughnu/2\354\243\274\354\260\250_\354\236\254\352\267\200_\353\260\261\355\212\270\353\236\230\355\202\271/Programmers_42883.swift" rename to "snughnu/5\354\243\274\354\260\250_\352\267\270\353\246\254\353\224\224/Programmers_42883.swift" diff --git "a/snughnu/6\354\243\274\354\260\250_\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/11286.swift" "b/snughnu/6\354\243\274\354\260\250_\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/11286.swift" new file mode 100644 index 0000000..8264e31 --- /dev/null +++ "b/snughnu/6\354\243\274\354\260\250_\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/11286.swift" @@ -0,0 +1,100 @@ +// 백준_11286 절댓값 힙 +/* + 오답1. 절대값이 같은 값이 여러 개일 경우에는 작은 수를 먼저 출력해야함 + */ +func solution(_ calculations: [Int]) { + var heap: [Int] = [0] // heap[0]은 0으로 더미 값 + + func insert(_ value: Int) { + heap.append(value) + + /* 오답 1 + // bubble up + var index = heap.count - 1 + while index > 1 && abs(heap[index]) < abs(heap[index/2]) { + heap.swapAt(index, index/2) + index /= 2 + } + */ + + // bubble up + var index = heap.count - 1 + while index > 1 { + let parent = index / 2 + if abs(heap[index]) < abs(heap[parent]) || + (abs(heap[index]) == abs(heap[parent]) && + heap[index] < heap[parent]) { + heap.swapAt(index, parent) + index = parent + } else { + break + } + } + } + + func removeMin() -> Int { + if heap.count == 1 { + return 0 + } else if heap.count == 2 { + return heap.removeLast() + } else { + let minValue = heap[1] + heap[1] = heap.removeLast() + + /* + 오답1. + + // bubble down + var index: Int = 1 + while index*2 < heap.count { + var child = index * 2 + if child + 1 < heap.count && abs(heap[child+1]) < abs(heap[child]) { + child += 1 + } + + if abs(heap[index]) <= abs(heap[child]) { break } + + heap.swapAt(index, child) + index = child + } + + */ + var index: Int = 1 + while index * 2 < heap.count { + var child = index * 2 + if child + 1 < heap.count && + (abs(heap[child + 1]) < abs(heap[child]) || + (abs(heap[child + 1]) == abs(heap[child]) && + heap[child + 1] < heap[child])) { + child += 1 + } + + if abs(heap[index]) < abs(heap[child]) || + (abs(heap[index]) == abs(heap[child]) && + heap[index] <= heap[child]) { + break + } + + heap.swapAt(index, child) + index = child + } + + return minValue + } + } + + for calculation in calculations { + if calculation == 0 { + print(removeMin()) + } else { + insert(calculation) + } + } +} + +let n: Int = Int(readLine()!)! +var calculations: [Int] = [] +for _ in (0.. 1 && heap[index] < heap[index/2] { + heap.swapAt(index, index/2) + index /= 2 + } + } + + func removeMin() { + heap[1] = heap.removeLast() + + // bubble down + var index = 1 + while index*2 < heap.count { + var child = index*2 + if child + 1 < heap.count && + heap[child + 1] < heap[child] { + child += 1 + } + if heap[index] <= heap[child] { break } + + heap.swapAt(index, child) + index = child + } + } + + for array in arrays { + for num in array { + if heap.count <= n { + insert(num) + } else if num > heap[1] { + removeMin() + insert(num) + } + } + } + + print(heap[1]) +} + +// heap 구조체 정의 +struct MinHeap { + private var heap = [Int]() + + init() { + heap.append(0) + } + + var count: Int { + return heap.count - 1 + } + + var top: Int { + return heap[1] + } + + mutating func insert(_ value: Int) { + heap.append(value) + + // bubble up + var index = heap.count - 1 + while index > 1 && heap[index] < heap[index/2] { + heap.swapAt(index, index/2) + index /= 2 + } + } + + mutating func removeMin() { + guard heap.count > 1 else { return } + heap.swapAt(1, heap.count - 1) + _ = heap.removeLast() + + // bubble down + var index = 1 + while index*2 < heap.count { + var child = index * 2 + if child + 1 < heap.count && + heap[child + 1] < heap[child] { + child += 1 + } + if heap[index] <= heap[child] { break } + + heap.swapAt(index, child) + index = child + } + } +} + +func solution(_ arrays: [[Int]]) { + let n = arrays.count + var heap = MinHeap() + + for array in arrays { + for num in array { + if heap.count < n { + heap.insert(num) + } else if num > heap.top { + heap.removeMin() + heap.insert(num) + } + } + } + + print(heap.top) +} + +let n: Int = Int(readLine()!)! +var arrays: [[Int]] = [] +for _ in (0.. Int { + var count: Int = 0 + + for i in (1.. Int { + var count: Int = 0 + var leftSet: Set = [] + var rightDict: [Int: Int] = [:] + + // 먼저 모든 토핑을 rightDict에 저장 + for top in topping { + rightDict[top, default: 0] += 1 + } + + // 왼쪽에서 하나씩 늘려가면서 비교하기 + for i in (0.. [Int] { + // 남은 배포일 계산 + var days: [Int] = [] + for i in 0.. [Int] { + var days: [Int] = [] + for i in 0..