为有牺牲多壮志,敢教日月换新天。

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

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10473321.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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.

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

Example:

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 天开始。

给出 n 个在线课程用 (t, d) 对表示。你的任务是找出最多可以修几门课。 

示例:

输入: [[100, 200], [200, 1300], [1000, 1250], [2000, 3200]]
输出: 3
解释: 
这里一共有 4 门课程, 但是你最多可以修 3 门:
首先, 修第一门课时, 它要耗费 100 天,你会在第 100 天完成, 在第 101 天准备下门课。
第二, 修第三门课时, 它会耗费 1000 天,所以你将在第 1100 天的时候完成它, 以及在第 1101 天开始准备下门课程。
第三, 修第二门课时, 它会耗时 200 天,所以你将会在第 1300 天时完成它。
第四门课现在不能修,因为你将会在第 3300 天完成它,这已经超出了关闭日期。 

提示:

  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 }

 

posted @ 2019-03-04 21:26  为敢技术  阅读(517)  评论(0编辑  收藏  举报