Skip to content

Commit a377fa3

Browse files
committed
feat: add solutions to lc problem: No.0621
No.0621.Task Scheduler
1 parent 556ec1e commit a377fa3

File tree

6 files changed

+221
-2
lines changed

6 files changed

+221
-2
lines changed

solution/0600-0699/0621.Task Scheduler/README.md

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,105 @@
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

61+
**方法一:贪心 + 构造**
62+
63+
不妨设 $m$ 是任务的个数,统计每种任务出现的次数,记录在数组 `cnt` 中。
64+
65+
假设出现次数最多的任务为 `A`,出现次数为 $x$,则至少需要 $(x-1)\times(n+1) + 1$ 个时间单位才能安排完所有任务。如果出现次数最多的任务有 $s$ 个,则需要再加上出现次数最多的任务的个数。
66+
67+
答案是 $\max ((x-1) \times(n+1)+s, m)$。
68+
69+
时间复杂度 $O(m+|\Sigma|)$。其中 $|\Sigma|$ 是任务的种类数。
70+
6171
<!-- tabs:start -->
6272

6373
### **Python3**
6474

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

6777
```python
68-
78+
class Solution:
79+
def leastInterval(self, tasks: List[str], n: int) -> int:
80+
cnt = Counter(tasks)
81+
x = max(cnt.values())
82+
s = sum(v == x for v in cnt.values())
83+
return max(len(tasks), (x - 1) * (n + 1) + s)
6984
```
7085

7186
### **Java**
7287

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

7590
```java
91+
class Solution {
92+
public int leastInterval(char[] tasks, int n) {
93+
int[] cnt = new int[26];
94+
int x = 0;
95+
for (char c : tasks) {
96+
c -= 'A';
97+
++cnt[c];
98+
x = Math.max(x, cnt[c]);
99+
}
100+
int s = 0;
101+
for (int v : cnt) {
102+
if (v == x) {
103+
++s;
104+
}
105+
}
106+
return Math.max(tasks.length, (x - 1) * (n + 1) + s);
107+
}
108+
}
109+
```
110+
111+
### **C++**
112+
113+
```cpp
114+
class Solution {
115+
public:
116+
int leastInterval(vector<char>& tasks, int n) {
117+
vector<int> cnt(26);
118+
int x = 0;
119+
for (char c : tasks) {
120+
c -= 'A';
121+
++cnt[c];
122+
x = max(x, cnt[c]);
123+
}
124+
int s = 0;
125+
for (int v : cnt) {
126+
s += v == x;
127+
}
128+
return max((int) tasks.size(), (x - 1) * (n + 1) + s);
129+
}
130+
};
131+
```
76132
133+
### **Go**
134+
135+
136+
```go
137+
func leastInterval(tasks []byte, n int) int {
138+
cnt := make([]int, 26)
139+
x := 0
140+
for _, c := range tasks {
141+
c -= 'A'
142+
cnt[c]++
143+
x = max(x, cnt[c])
144+
}
145+
s := 0
146+
for _, v := range cnt {
147+
if v == x {
148+
s++
149+
}
150+
}
151+
return max(len(tasks), (x-1)*(n+1)+s)
152+
}
153+
154+
func max(a, b int) int {
155+
if a > b {
156+
return a
157+
}
158+
return b
159+
}
77160
```
78161

79162
### **...**

solution/0600-0699/0621.Task Scheduler/README_EN.md

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,85 @@ A -&gt; B -&gt; C -&gt; A -&gt; D -&gt; E -&gt; A -&gt; F -&gt; G -&gt; A -&gt;
6060
### **Python3**
6161

6262
```python
63-
63+
class Solution:
64+
def leastInterval(self, tasks: List[str], n: int) -> int:
65+
cnt = Counter(tasks)
66+
x = max(cnt.values())
67+
s = sum(v == x for v in cnt.values())
68+
return max(len(tasks), (x - 1) * (n + 1) + s)
6469
```
6570

6671
### **Java**
6772

6873
```java
74+
class Solution {
75+
public int leastInterval(char[] tasks, int n) {
76+
int[] cnt = new int[26];
77+
int x = 0;
78+
for (char c : tasks) {
79+
c -= 'A';
80+
++cnt[c];
81+
x = Math.max(x, cnt[c]);
82+
}
83+
int s = 0;
84+
for (int v : cnt) {
85+
if (v == x) {
86+
++s;
87+
}
88+
}
89+
return Math.max(tasks.length, (x - 1) * (n + 1) + s);
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
int leastInterval(vector<char>& tasks, int n) {
100+
vector<int> cnt(26);
101+
int x = 0;
102+
for (char c : tasks) {
103+
c -= 'A';
104+
++cnt[c];
105+
x = max(x, cnt[c]);
106+
}
107+
int s = 0;
108+
for (int v : cnt) {
109+
s += v == x;
110+
}
111+
return max((int) tasks.size(), (x - 1) * (n + 1) + s);
112+
}
113+
};
114+
```
69115
116+
### **Go**
117+
118+
```go
119+
func leastInterval(tasks []byte, n int) int {
120+
cnt := make([]int, 26)
121+
x := 0
122+
for _, c := range tasks {
123+
c -= 'A'
124+
cnt[c]++
125+
x = max(x, cnt[c])
126+
}
127+
s := 0
128+
for _, v := range cnt {
129+
if v == x {
130+
s++
131+
}
132+
}
133+
return max(len(tasks), (x-1)*(n+1)+s)
134+
}
135+
136+
func max(a, b int) int {
137+
if a > b {
138+
return a
139+
}
140+
return b
141+
}
70142
```
71143

72144
### **...**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int leastInterval(vector<char>& tasks, int n) {
4+
vector<int> cnt(26);
5+
int x = 0;
6+
for (char c : tasks) {
7+
c -= 'A';
8+
++cnt[c];
9+
x = max(x, cnt[c]);
10+
}
11+
int s = 0;
12+
for (int v : cnt) {
13+
s += v == x;
14+
}
15+
return max((int) tasks.size(), (x - 1) * (n + 1) + s);
16+
}
17+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func leastInterval(tasks []byte, n int) int {
2+
cnt := make([]int, 26)
3+
x := 0
4+
for _, c := range tasks {
5+
c -= 'A'
6+
cnt[c]++
7+
x = max(x, cnt[c])
8+
}
9+
s := 0
10+
for _, v := range cnt {
11+
if v == x {
12+
s++
13+
}
14+
}
15+
return max(len(tasks), (x-1)*(n+1)+s)
16+
}
17+
18+
func max(a, b int) int {
19+
if a > b {
20+
return a
21+
}
22+
return b
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int leastInterval(char[] tasks, int n) {
3+
int[] cnt = new int[26];
4+
int x = 0;
5+
for (char c : tasks) {
6+
c -= 'A';
7+
++cnt[c];
8+
x = Math.max(x, cnt[c]);
9+
}
10+
int s = 0;
11+
for (int v : cnt) {
12+
if (v == x) {
13+
++s;
14+
}
15+
}
16+
return Math.max(tasks.length, (x - 1) * (n + 1) + s);
17+
}
18+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def leastInterval(self, tasks: List[str], n: int) -> int:
3+
cnt = Counter(tasks)
4+
x = max(cnt.values())
5+
s = sum(v == x for v in cnt.values())
6+
return max(len(tasks), (x - 1) * (n + 1) + s)

0 commit comments

Comments
 (0)