|
58 | 58 | <li><code>1 <= servers[i], tasks[j] <= 2 * 10<sup>5</sup></code></li>
|
59 | 59 | </ul>
|
60 | 60 |
|
61 |
| - |
62 | 61 | ## 解法
|
63 | 62 |
|
64 | 63 | <!-- 这里可写通用的实现逻辑 -->
|
65 | 64 |
|
| 65 | +“优先队列”实现。 |
| 66 | + |
| 67 | +定义两个优先级队列,分别表示空闲服务器、使用中的服务器。其中:空闲服务器 `idle` 依据**权重、下标**排序;而使用中的服务器 `busy` 依据**结束时间、权重、下标**排序。 |
| 68 | + |
| 69 | +遍历任务: |
| 70 | + |
| 71 | +- 若有使用中的服务器小于任务开始时间,将其加入到空闲服务器队列 `idle` 中; |
| 72 | +- 若当前有空闲服务器,那么在空闲队列 `idle` 中取出权重最小的服务器,将其加入使用中的队列 `busy` 中; |
| 73 | +- 若当前没有空闲服务器,那么在使用队列 `busy` 中找出最早结束时间且权重最小的服务器,重新加入使用中的队列 `busy` 中。 |
| 74 | + |
66 | 75 | <!-- tabs:start -->
|
67 | 76 |
|
68 | 77 | ### **Python3**
|
69 | 78 |
|
70 | 79 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
71 | 80 |
|
72 | 81 | ```python
|
73 |
| - |
| 82 | +class Solution: |
| 83 | + def assignTasks(self, servers: List[int], tasks: List[int]) -> List[int]: |
| 84 | + idle, busy = [], [] |
| 85 | + for i, weight in enumerate(servers): |
| 86 | + heapq.heappush(idle, (weight, i)) |
| 87 | + res = [] |
| 88 | + for start, cost in enumerate(tasks): |
| 89 | + while busy and busy[0][0] <= start: |
| 90 | + _, s, i = heapq.heappop(busy) |
| 91 | + heapq.heappush(idle, (s, i)) |
| 92 | + if idle: |
| 93 | + s, i = heapq.heappop(idle) |
| 94 | + heapq.heappush(busy, (start + cost, s, i)) |
| 95 | + else: |
| 96 | + t, s, i = heapq.heappop(busy) |
| 97 | + heapq.heappush(busy, (t + cost, s, i)) |
| 98 | + res.append(i) |
| 99 | + return res |
74 | 100 | ```
|
75 | 101 |
|
76 | 102 | ### **Java**
|
77 | 103 |
|
78 | 104 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
79 | 105 |
|
80 | 106 | ```java
|
81 |
| - |
| 107 | +class Solution { |
| 108 | + public int[] assignTasks(int[] servers, int[] tasks) { |
| 109 | + int m = tasks.length, n = servers.length; |
| 110 | + PriorityQueue<int[]> idle = new PriorityQueue<>((a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]); |
| 111 | + PriorityQueue<int[]> busy = new PriorityQueue<>((a, b) -> { |
| 112 | + if (a[0] == b[0]) { |
| 113 | + return a[1] == b[1] ? a[2] - b[2] : a[1] - b[1]; |
| 114 | + } |
| 115 | + return a[0] - b[0]; |
| 116 | + }); |
| 117 | + for (int i = 0; i < n; ++i) { |
| 118 | + idle.offer(new int[]{servers[i], i}); |
| 119 | + } |
| 120 | + int[] res = new int[m]; |
| 121 | + int j = 0; |
| 122 | + for (int start = 0; start < m; ++start) { |
| 123 | + int cost = tasks[start]; |
| 124 | + while (!busy.isEmpty() && busy.peek()[0] <= start) { |
| 125 | + int[] item = busy.poll(); |
| 126 | + idle.offer(new int[]{item[1], item[2]}); |
| 127 | + } |
| 128 | + if (!idle.isEmpty()) { |
| 129 | + int[] item = idle.poll(); |
| 130 | + res[j++] = item[1]; |
| 131 | + busy.offer(new int[]{start + cost, item[0], item[1]}); |
| 132 | + } else { |
| 133 | + int[] item = busy.poll(); |
| 134 | + res[j++] = item[2]; |
| 135 | + busy.offer(new int[]{item[0] + cost, item[1], item[2]}); |
| 136 | + } |
| 137 | + } |
| 138 | + return res; |
| 139 | + } |
| 140 | +} |
82 | 141 | ```
|
83 | 142 |
|
84 | 143 | ### **...**
|
|
0 commit comments