[Swift]LeetCode630. 課程表 III | Course Schedule III

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-fizhyurp-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

There are n different online courses numbered from 1 to n. Each course has some duration(course length) t and closed on dth day. A course should be taken continuouslyfor t days and must be finished before or on the dth day. You will start at the 1st day.git

Given n online courses represented by pairs (t,d), your task is to find the maximal number of courses that can be taken.github

Example:api

Input: [[100, 200], [200, 1300], [1000, 1250], [2000, 3200]]
Output: 3
Explanation: 
There're totally 4 courses, but you can take 3 courses at most:
First, take the 1st course, it costs 100 days so you will finish it on the 100th day, and ready to take the next course on the 101st day.
Second, take the 3rd course, it costs 1000 days so you will finish it on the 1100th day, and ready to take the next course on the 1101st day. 
Third, take the 2nd course, it costs 200 days so you will finish it on the 1300th day. 
The 4th course cannot be taken now, since you will finish it on the 3300th day, which exceeds the closed date.

Note:微信

  1. The integer 1 <= d, t, n <= 10,000.
  2. You can't take two courses simultaneously.

這裏有 n 門不一樣的在線課程,他們按從 1 到 n 編號。每一門課程有必定的持續上課時間(課程時間)t 以及關閉時間第 d 天。一門課要持續學習 t 天直到第 d 天時要完成,你將會從第 1 天開始。app

給出 n 個在線課程用 (t, d) 對錶示。你的任務是找出最多能夠修幾門課。 學習

示例:spa

輸入: [[100, 200], [200, 1300], [1000, 1250], [2000, 3200]]
輸出: 3
解釋: 
這裏一共有 4 門課程, 可是你最多能夠修 3 門:
首先, 修第一門課時, 它要耗費 100 天,你會在第 100 天完成, 在第 101 天準備下門課。
第二, 修第三門課時, 它會耗費 1000 天,因此你將在第 1100 天的時候完成它, 以及在第 1101 天開始準備下門課程。
第三, 修第二門課時, 它會耗時 200 天,因此你將會在第 1300 天時完成它。
第四門課如今不能修,由於你將會在第 3300 天完成它,這已經超出了關閉日期。 

提示:code

  1. 整數 1 <= d, t, n <= 10,000 。
  2. 你不能同時修兩門課程。

Runtime: 1216 ms
Memory Usage: 19.6 MB
  1 class Solution {
  2     func scheduleCourse(_ courses: [[Int]]) -> Int {
  3         var courses = courses
  4         var curTime:Int = 0
  5         var priorityQueue = PriorityQueue<Int>(order: >)
  6         courses.sort(by:{
  7             (a:[Int],b:[Int]) -> Bool in
  8             return a[1] < b[1]
  9         })
 10         for course in courses
 11         {
 12             curTime += course[0]
 13             priorityQueue.enqueue(course[0])
 14             if curTime > course[1],let num:Int? = priorityQueue.dequeue()
 15             {
 16                 if num != nil
 17                 {
 18                     curTime -= num!
 19                 }                                                            
 20             }
 21         }
 22         return priorityQueue.count       
 23     }
 24 }
 25 
 26 struct PriorityQueue<Element: Equatable> {
 27     private var heap: Heap<Element>
 28     
 29     init(order: @escaping (Element, Element) -> Bool) {
 30         heap = Heap(order: order)
 31     }
 32     
 33     var isEmpty: Bool {
 34         return heap.isEmpty
 35     }
 36     
 37     var count:Int
 38     {
 39         return heap.count
 40     }
 41     
 42     var peek: Element? {
 43         return heap.peek
 44     }
 45     
 46     mutating func enqueue(_ element: Element) {
 47         heap.insert(element)
 48     }
 49     
 50     mutating func dequeue() -> Element? {
 51         return heap.removePeek()
 52     }
 53 }
 54 
 55 extension PriorityQueue: CustomStringConvertible {
 56     var description: String {
 57         return heap.description
 58     }
 59 }
 60 
 61 struct Heap<Element: Equatable> {
 62     private(set) var elements: [Element] = []
 63     private let order: (Element, Element) -> Bool
 64 
 65     init(order: @escaping (Element, Element) -> Bool) {
 66         self.order = order
 67     }
 68 
 69     var isEmpty: Bool {
 70         return elements.isEmpty
 71     }
 72 
 73     var count: Int {
 74         return elements.count
 75     }
 76 
 77     var peek: Element? {
 78         return elements.first
 79     }
 80 
 81     func leftChildIndex(ofParentAt index: Int) -> Int {
 82         return 2 * index + 1
 83     }
 84 
 85     func rightChildIndex(ofParentAt index: Int) -> Int {
 86         return 2 * index + 2
 87     }
 88 
 89     func parentIndex(ofChildAt index: Int) -> Int {
 90         return (index - 1) / 2
 91     }
 92 }
 93 
 94 extension Heap: CustomStringConvertible {
 95     var description: String {
 96         return elements.description
 97     }
 98 }
 99 
100 // MARK: - Remove & Insert
101 extension Heap {
102     @discardableResult
103     mutating func removePeek() -> Element? {
104         guard !isEmpty else {
105             return nil
106         }
107         elements.swapAt(0, count - 1)
108         defer {
109             validateDown(from: 0)
110         }
111         return elements.removeLast()
112     }
113 
114     @discardableResult
115     mutating func remove(at index: Int) -> Element? {
116         guard index < elements.count else {
117             return nil
118         }
119         if index == elements.count - 1 {
120             return elements.removeLast()
121         } else {
122             elements.swapAt(index, elements.count - 1)
123             defer {
124                 validateDown(from: index)
125                 validateUp(from: index)
126             }
127             return elements.removeLast()
128         }
129     }
130 
131     mutating func insert(_ element: Element) {
132         elements.append(element)
133         validateUp(from: elements.count - 1)
134     }
135 
136     private mutating func validateUp(from index: Int) {
137         var childIndex = index
138         var parentIndex = self.parentIndex(ofChildAt: childIndex)
139 
140         while childIndex > 0 &&
141             order(elements[childIndex], elements[parentIndex]) {
142                 elements.swapAt(childIndex, parentIndex)
143                 childIndex = parentIndex
144                 parentIndex = self.parentIndex(ofChildAt: childIndex)
145         }
146     }
147 
148     private mutating func validateDown(from index: Int) {
149         var parentIndex = index
150         while true {
151             let leftIndex = leftChildIndex(ofParentAt: parentIndex)
152             let rightIndex = rightChildIndex(ofParentAt: parentIndex)
153             var targetParentIndex = parentIndex
154             
155             if leftIndex < count &&
156                 order(elements[leftIndex], elements[targetParentIndex]) {
157                 targetParentIndex = leftIndex
158             }
159             
160             if rightIndex < count &&
161                 order(elements[rightIndex], elements[targetParentIndex]) {
162                 targetParentIndex = rightIndex
163             }
164             
165             if targetParentIndex == parentIndex {
166                 return
167             }
168             
169             elements.swapAt(parentIndex, targetParentIndex)
170             parentIndex = targetParentIndex
171         }
172     }
173 }
174 
175 // MARK: - Search
176 extension Heap {
177     func index(of element: Element,
178                searchingFrom index: Int = 0) -> Int? {
179         if index >= count {
180             return nil
181         }
182         if order(element, elements[index]) {
183             return nil
184         }
185         if element == elements[index] {
186             return index
187         }
188         
189         let leftIndex = leftChildIndex(ofParentAt: index)
190         if let i = self.index(of: element,
191                               searchingFrom: leftIndex) {
192             return i
193         }
194         
195         let rightIndex = rightChildIndex(ofParentAt: index)
196         if let i = self.index(of: element,
197                               searchingFrom: rightIndex) {
198             return i
199         }
200         
201         return nil
202     }
203 }
相關文章
相關標籤/搜索