Skip to content

Commit 43eceeb

Browse files
committed
feat: add solutions to lc problem: No.1882. Process Tasks Using Servers
1 parent c18a8f4 commit 43eceeb

File tree

9 files changed

+184
-23
lines changed

9 files changed

+184
-23
lines changed

solution/1800-1899/1880.Check if Word Equals Summation of Two Words/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ targetWord 的数值为 "aaaa" -> "0000" -> 0
7474
```python
7575
class Solution:
7676
def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:
77-
def transfer(word):
77+
def convert(word):
7878
res = 0
7979
for c in word:
8080
res *= 10
8181
res += (ord(c) - ord('a'))
8282
return res
83-
return transfer(firstWord) + transfer(secondWord) == transfer(targetWord)
83+
return convert(firstWord) + convert(secondWord) == convert(targetWord)
8484
```
8585

8686
### **Java**
@@ -90,10 +90,10 @@ class Solution:
9090
```java
9191
class Solution {
9292
public boolean isSumEqual(String firstWord, String secondWord, String targetWord) {
93-
return transfer(firstWord) + transfer(secondWord) == transfer(targetWord);
93+
return convert(firstWord) + convert(secondWord) == convert(targetWord);
9494
}
9595

96-
private int transfer(String word) {
96+
private int convert(String word) {
9797
int res = 0;
9898
for (char c : word.toCharArray()) {
9999
res *= 10;
@@ -110,10 +110,10 @@ class Solution {
110110
class Solution {
111111
public:
112112
bool isSumEqual(string firstWord, string secondWord, string targetWord) {
113-
return transfer(firstWord) + transfer(secondWord) == transfer(targetWord);
113+
return convert(firstWord) + convert(secondWord) == convert(targetWord);
114114
}
115115
private:
116-
int transfer(string word) {
116+
int convert(string word) {
117117
int res = 0;
118118
for (char c : word) {
119119
res *= 10;

solution/1800-1899/1880.Check if Word Equals Summation of Two Words/README_EN.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -96,24 +96,24 @@ We return true because 0 + 0 == 0.
9696
```python
9797
class Solution:
9898
def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:
99-
def transfer(word):
99+
def convert(word):
100100
res = 0
101101
for c in word:
102102
res *= 10
103103
res += (ord(c) - ord('a'))
104104
return res
105-
return transfer(firstWord) + transfer(secondWord) == transfer(targetWord)
105+
return convert(firstWord) + convert(secondWord) == convert(targetWord)
106106
```
107107

108108
### **Java**
109109

110110
```java
111111
class Solution {
112112
public boolean isSumEqual(String firstWord, String secondWord, String targetWord) {
113-
return transfer(firstWord) + transfer(secondWord) == transfer(targetWord);
113+
return convert(firstWord) + convert(secondWord) == convert(targetWord);
114114
}
115115

116-
private int transfer(String word) {
116+
private int convert(String word) {
117117
int res = 0;
118118
for (char c : word.toCharArray()) {
119119
res *= 10;
@@ -130,10 +130,10 @@ class Solution {
130130
class Solution {
131131
public:
132132
bool isSumEqual(string firstWord, string secondWord, string targetWord) {
133-
return transfer(firstWord) + transfer(secondWord) == transfer(targetWord);
133+
return convert(firstWord) + convert(secondWord) == convert(targetWord);
134134
}
135135
private:
136-
int transfer(string word) {
136+
int convert(string word) {
137137
int res = 0;
138138
for (char c : word) {
139139
res *= 10;

solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class Solution {
22
public:
33
bool isSumEqual(string firstWord, string secondWord, string targetWord) {
4-
return transfer(firstWord) + transfer(secondWord) == transfer(targetWord);
4+
return convert(firstWord) + convert(secondWord) == convert(targetWord);
55
}
66
private:
7-
int transfer(string word) {
7+
int convert(string word) {
88
int res = 0;
99
for (char c : word) {
1010
res *= 10;

solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class Solution {
22
public boolean isSumEqual(String firstWord, String secondWord, String targetWord) {
3-
return transfer(firstWord) + transfer(secondWord) == transfer(targetWord);
3+
return convert(firstWord) + convert(secondWord) == convert(targetWord);
44
}
55

6-
private int transfer(String word) {
6+
private int convert(String word) {
77
int res = 0;
88
for (char c : word.toCharArray()) {
99
res *= 10;
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class Solution:
22
def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:
3-
def transfer(word):
3+
def convert(word):
44
res = 0
55
for c in word:
66
res *= 10
77
res += (ord(c) - ord('a'))
88
return res
9-
return transfer(firstWord) + transfer(secondWord) == transfer(targetWord)
9+
return convert(firstWord) + convert(secondWord) == convert(targetWord)

solution/1800-1899/1882.Process Tasks Using Servers/README.md

+62-3
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,86 @@
5858
<li><code>1 <= servers[i], tasks[j] <= 2 * 10<sup>5</sup></code></li>
5959
</ul>
6060

61-
6261
## 解法
6362

6463
<!-- 这里可写通用的实现逻辑 -->
6564

65+
“优先队列”实现。
66+
67+
定义两个优先级队列,分别表示空闲服务器、使用中的服务器。其中:空闲服务器 `idle` 依据**权重、下标**排序;而使用中的服务器 `busy` 依据**结束时间、权重、下标**排序。
68+
69+
遍历任务:
70+
71+
- 若有使用中的服务器小于任务开始时间,将其加入到空闲服务器队列 `idle` 中;
72+
- 若当前有空闲服务器,那么在空闲队列 `idle` 中取出权重最小的服务器,将其加入使用中的队列 `busy` 中;
73+
- 若当前没有空闲服务器,那么在使用队列 `busy` 中找出最早结束时间且权重最小的服务器,重新加入使用中的队列 `busy` 中。
74+
6675
<!-- tabs:start -->
6776

6877
### **Python3**
6978

7079
<!-- 这里可写当前语言的特殊实现逻辑 -->
7180

7281
```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
74100
```
75101

76102
### **Java**
77103

78104
<!-- 这里可写当前语言的特殊实现逻辑 -->
79105

80106
```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+
}
82141
```
83142

84143
### **...**

solution/1800-1899/1882.Process Tasks Using Servers/README_EN.md

+52-2
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,63 @@
6363
### **Python3**
6464

6565
```python
66-
66+
class Solution:
67+
def assignTasks(self, servers: List[int], tasks: List[int]) -> List[int]:
68+
idle, busy = [], []
69+
for i, weight in enumerate(servers):
70+
heapq.heappush(idle, (weight, i))
71+
res = []
72+
for start, cost in enumerate(tasks):
73+
while busy and busy[0][0] <= start:
74+
_, s, i = heapq.heappop(busy)
75+
heapq.heappush(idle, (s, i))
76+
if idle:
77+
s, i = heapq.heappop(idle)
78+
heapq.heappush(busy, (start + cost, s, i))
79+
else:
80+
t, s, i = heapq.heappop(busy)
81+
heapq.heappush(busy, (t + cost, s, i))
82+
res.append(i)
83+
return res
6784
```
6885

6986
### **Java**
7087

7188
```java
72-
89+
class Solution {
90+
public int[] assignTasks(int[] servers, int[] tasks) {
91+
int m = tasks.length, n = servers.length;
92+
PriorityQueue<int[]> idle = new PriorityQueue<>((a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
93+
PriorityQueue<int[]> busy = new PriorityQueue<>((a, b) -> {
94+
if (a[0] == b[0]) {
95+
return a[1] == b[1] ? a[2] - b[2] : a[1] - b[1];
96+
}
97+
return a[0] - b[0];
98+
});
99+
for (int i = 0; i < n; ++i) {
100+
idle.offer(new int[]{servers[i], i});
101+
}
102+
int[] res = new int[m];
103+
int j = 0;
104+
for (int start = 0; start < m; ++start) {
105+
int cost = tasks[start];
106+
while (!busy.isEmpty() && busy.peek()[0] <= start) {
107+
int[] item = busy.poll();
108+
idle.offer(new int[]{item[1], item[2]});
109+
}
110+
if (!idle.isEmpty()) {
111+
int[] item = idle.poll();
112+
res[j++] = item[1];
113+
busy.offer(new int[]{start + cost, item[0], item[1]});
114+
} else {
115+
int[] item = busy.poll();
116+
res[j++] = item[2];
117+
busy.offer(new int[]{item[0] + cost, item[1], item[2]});
118+
}
119+
}
120+
return res;
121+
}
122+
}
73123
```
74124

75125
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public int[] assignTasks(int[] servers, int[] tasks) {
3+
int m = tasks.length, n = servers.length;
4+
PriorityQueue<int[]> idle = new PriorityQueue<>((a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
5+
PriorityQueue<int[]> busy = new PriorityQueue<>((a, b) -> {
6+
if (a[0] == b[0]) {
7+
return a[1] == b[1] ? a[2] - b[2] : a[1] - b[1];
8+
}
9+
return a[0] - b[0];
10+
});
11+
for (int i = 0; i < n; ++i) {
12+
idle.offer(new int[]{servers[i], i});
13+
}
14+
int[] res = new int[m];
15+
int j = 0;
16+
for (int start = 0; start < m; ++start) {
17+
int cost = tasks[start];
18+
while (!busy.isEmpty() && busy.peek()[0] <= start) {
19+
int[] item = busy.poll();
20+
idle.offer(new int[]{item[1], item[2]});
21+
}
22+
if (!idle.isEmpty()) {
23+
int[] item = idle.poll();
24+
res[j++] = item[1];
25+
busy.offer(new int[]{start + cost, item[0], item[1]});
26+
} else {
27+
int[] item = busy.poll();
28+
res[j++] = item[2];
29+
busy.offer(new int[]{item[0] + cost, item[1], item[2]});
30+
}
31+
}
32+
return res;
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def assignTasks(self, servers: List[int], tasks: List[int]) -> List[int]:
3+
idle, busy = [], []
4+
for i, weight in enumerate(servers):
5+
heapq.heappush(idle, (weight, i))
6+
res = []
7+
for start, cost in enumerate(tasks):
8+
while busy and busy[0][0] <= start:
9+
_, s, i = heapq.heappop(busy)
10+
heapq.heappush(idle, (s, i))
11+
if idle:
12+
s, i = heapq.heappop(idle)
13+
heapq.heappush(busy, (start + cost, s, i))
14+
else:
15+
t, s, i = heapq.heappop(busy)
16+
heapq.heappush(busy, (t + cost, s, i))
17+
res.append(i)
18+
return res

0 commit comments

Comments
 (0)