Skip to content

Commit b77b37b

Browse files
committed
feat: add solution to lc problem: No.2365
No.2365.Task Scheduler II
1 parent a7a3d1e commit b77b37b

File tree

8 files changed

+107
-94
lines changed

8 files changed

+107
-94
lines changed

solution/2300-2399/2363.Merge Similar Items/README.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,21 +145,20 @@ public:
145145
### **Go**
146146
147147
```go
148-
func mergeSimilarItems(items1 [][]int, items2 [][]int) [][]int {
149-
cnt := make([]int, 1010)
150-
for _, e := range items1 {
151-
cnt[e[0]] += e[1]
148+
func mergeSimilarItems(items1 [][]int, items2 [][]int) (ans [][]int) {
149+
cnt := [1010]int{}
150+
for _, x := range items1 {
151+
cnt[x[0]] += x[1]
152152
}
153-
for _, e := range items2 {
154-
cnt[e[0]] += e[1]
153+
for _, x := range items2 {
154+
cnt[x[0]] += x[1]
155155
}
156-
ans := [][]int{}
157-
for i, v := range cnt {
158-
if v > 0 {
159-
ans = append(ans, []int{i, v})
156+
for i, x := range cnt {
157+
if x > 0 {
158+
ans = append(ans, []int{i, x})
160159
}
161160
}
162-
return ans
161+
return
163162
}
164163
```
165164

solution/2300-2399/2365.Task Scheduler II/README.md

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@
6868

6969
<!-- 这里可写通用的实现逻辑 -->
7070

71+
**方法一:哈希表 + 模拟**
72+
73+
我们可以用哈希表 $day$ 记录每个任务下一次可以被执行的时间,初始时 $day$ 中的所有值都为 $0$,用变量 $ans$ 记录当前时间。
74+
75+
遍历数组 $tasks$,对于每个任务 $task$,当前时间 $ans$ 加一,表示从上一次执行任务到现在已经过去了一天,如果此时 $day[task] \gt ans$,说明任务 $task$ 需要在第 $day[task]$ 天才能被执行,因此我们更新当前时间 $ans = max(ans, day[task])$。然后更新 $day[task]$ 的值为 $ans + space + 1$,表示任务 $task$ 下一次可以被执行的时间为 $ans + space + 1$。
76+
77+
遍历结束后,将 $ans$ 返回即可。
78+
79+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $tasks$ 的长度。
80+
7181
<!-- tabs:start -->
7282

7383
### **Python3**
@@ -77,12 +87,12 @@
7787
```python
7888
class Solution:
7989
def taskSchedulerII(self, tasks: List[int], space: int) -> int:
80-
mp = {}
90+
day = defaultdict(int)
8191
ans = 0
82-
for v in tasks:
92+
for task in tasks:
8393
ans += 1
84-
ans = max(ans, mp.get(v, 0))
85-
mp[v] = ans + space + 1
94+
ans = max(ans, day[task])
95+
day[task] = ans + space + 1
8696
return ans
8797
```
8898

@@ -93,12 +103,12 @@ class Solution:
93103
```java
94104
class Solution {
95105
public long taskSchedulerII(int[] tasks, int space) {
96-
Map<Integer, Long> mp = new HashMap<>();
106+
Map<Integer, Long> day = new HashMap<>();
97107
long ans = 0;
98-
for (int v : tasks) {
108+
for (int task : tasks) {
99109
++ans;
100-
ans = Math.max(ans, mp.getOrDefault(v, 0L));
101-
mp.put(v, ans + space + 1);
110+
ans = Math.max(ans, day.getOrDefault(task, 0L));
111+
day.put(task, ans + space + 1);
102112
}
103113
return ans;
104114
}
@@ -111,12 +121,12 @@ class Solution {
111121
class Solution {
112122
public:
113123
long long taskSchedulerII(vector<int>& tasks, int space) {
114-
unordered_map<int, long long> mp;
124+
unordered_map<int, long long> day;
115125
long long ans = 0;
116-
for (int v : tasks) {
126+
for (int& task : tasks) {
117127
++ans;
118-
if (mp.count(v)) ans = max(ans, mp[v]);
119-
mp[v] = ans + space + 1;
128+
ans = max(ans, day[task]);
129+
day[task] = ans + space + 1;
120130
}
121131
return ans;
122132
}
@@ -126,31 +136,32 @@ public:
126136
### **Go**
127137
128138
```go
129-
func taskSchedulerII(tasks []int, space int) int64 {
130-
mp := map[int]int64{}
131-
var ans int64
132-
for _, x := range tasks {
139+
func taskSchedulerII(tasks []int, space int) (ans int64) {
140+
day := map[int]int64{}
141+
for _, task := range tasks {
133142
ans++
134-
if v, ok := mp[x]; ok {
135-
ans = max(ans, v)
143+
if ans < day[task] {
144+
ans = day[task]
136145
}
137-
mp[x] = ans + int64(space) + 1
146+
day[task] = ans + int64(space) + 1
138147
}
139-
return ans
140-
}
141-
142-
func max(a, b int64) int64 {
143-
if a > b {
144-
return a
145-
}
146-
return b
148+
return
147149
}
148150
```
149151

150152
### **TypeScript**
151153

152154
```ts
153-
155+
function taskSchedulerII(tasks: number[], space: number): number {
156+
const day = new Map<number, number>();
157+
let ans = 0;
158+
for (const task of tasks) {
159+
++ans;
160+
ans = Math.max(ans, day.get(task) ?? 0);
161+
day.set(task, ans + space + 1);
162+
}
163+
return ans;
164+
}
154165
```
155166

156167
### **...**

solution/2300-2399/2365.Task Scheduler II/README_EN.md

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ It can be shown that the tasks cannot be completed in less than 6 days.
7171
```python
7272
class Solution:
7373
def taskSchedulerII(self, tasks: List[int], space: int) -> int:
74-
mp = {}
74+
day = defaultdict(int)
7575
ans = 0
76-
for v in tasks:
76+
for task in tasks:
7777
ans += 1
78-
ans = max(ans, mp.get(v, 0))
79-
mp[v] = ans + space + 1
78+
ans = max(ans, day[task])
79+
day[task] = ans + space + 1
8080
return ans
8181
```
8282

@@ -85,12 +85,12 @@ class Solution:
8585
```java
8686
class Solution {
8787
public long taskSchedulerII(int[] tasks, int space) {
88-
Map<Integer, Long> mp = new HashMap<>();
88+
Map<Integer, Long> day = new HashMap<>();
8989
long ans = 0;
90-
for (int v : tasks) {
90+
for (int task : tasks) {
9191
++ans;
92-
ans = Math.max(ans, mp.getOrDefault(v, 0L));
93-
mp.put(v, ans + space + 1);
92+
ans = Math.max(ans, day.getOrDefault(task, 0L));
93+
day.put(task, ans + space + 1);
9494
}
9595
return ans;
9696
}
@@ -103,12 +103,12 @@ class Solution {
103103
class Solution {
104104
public:
105105
long long taskSchedulerII(vector<int>& tasks, int space) {
106-
unordered_map<int, long long> mp;
106+
unordered_map<int, long long> day;
107107
long long ans = 0;
108-
for (int v : tasks) {
108+
for (int& task : tasks) {
109109
++ans;
110-
if (mp.count(v)) ans = max(ans, mp[v]);
111-
mp[v] = ans + space + 1;
110+
ans = max(ans, day[task]);
111+
day[task] = ans + space + 1;
112112
}
113113
return ans;
114114
}
@@ -118,31 +118,32 @@ public:
118118
### **Go**
119119
120120
```go
121-
func taskSchedulerII(tasks []int, space int) int64 {
122-
mp := map[int]int64{}
123-
var ans int64
124-
for _, x := range tasks {
121+
func taskSchedulerII(tasks []int, space int) (ans int64) {
122+
day := map[int]int64{}
123+
for _, task := range tasks {
125124
ans++
126-
if v, ok := mp[x]; ok {
127-
ans = max(ans, v)
125+
if ans < day[task] {
126+
ans = day[task]
128127
}
129-
mp[x] = ans + int64(space) + 1
128+
day[task] = ans + int64(space) + 1
130129
}
131-
return ans
132-
}
133-
134-
func max(a, b int64) int64 {
135-
if a > b {
136-
return a
137-
}
138-
return b
130+
return
139131
}
140132
```
141133

142134
### **TypeScript**
143135

144136
```ts
145-
137+
function taskSchedulerII(tasks: number[], space: number): number {
138+
const day = new Map<number, number>();
139+
let ans = 0;
140+
for (const task of tasks) {
141+
++ans;
142+
ans = Math.max(ans, day.get(task) ?? 0);
143+
day.set(task, ans + space + 1);
144+
}
145+
return ans;
146+
}
146147
```
147148

148149
### **...**

solution/2300-2399/2365.Task Scheduler II/Solution.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class Solution {
22
public:
33
long long taskSchedulerII(vector<int>& tasks, int space) {
4-
unordered_map<int, long long> mp;
4+
unordered_map<int, long long> day;
55
long long ans = 0;
6-
for (int v : tasks) {
6+
for (int& task : tasks) {
77
++ans;
8-
if (mp.count(v)) ans = max(ans, mp[v]);
9-
mp[v] = ans + space + 1;
8+
ans = max(ans, day[task]);
9+
day[task] = ans + space + 1;
1010
}
1111
return ans;
1212
}
Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
1-
func taskSchedulerII(tasks []int, space int) int64 {
2-
mp := map[int]int64{}
3-
var ans int64
4-
for _, x := range tasks {
1+
func taskSchedulerII(tasks []int, space int) (ans int64) {
2+
day := map[int]int64{}
3+
for _, task := range tasks {
54
ans++
6-
if v, ok := mp[x]; ok {
7-
ans = max(ans, v)
5+
if ans < day[task] {
6+
ans = day[task]
87
}
9-
mp[x] = ans + int64(space) + 1
8+
day[task] = ans + int64(space) + 1
109
}
11-
return ans
12-
}
13-
14-
func max(a, b int64) int64 {
15-
if a > b {
16-
return a
17-
}
18-
return b
10+
return
1911
}

solution/2300-2399/2365.Task Scheduler II/Solution.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution {
22
public long taskSchedulerII(int[] tasks, int space) {
3-
Map<Integer, Long> mp = new HashMap<>();
3+
Map<Integer, Long> day = new HashMap<>();
44
long ans = 0;
5-
for (int v : tasks) {
5+
for (int task : tasks) {
66
++ans;
7-
ans = Math.max(ans, mp.getOrDefault(v, 0L));
8-
mp.put(v, ans + space + 1);
7+
ans = Math.max(ans, day.getOrDefault(task, 0L));
8+
day.put(task, ans + space + 1);
99
}
1010
return ans;
1111
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class Solution:
22
def taskSchedulerII(self, tasks: List[int], space: int) -> int:
3-
mp = {}
3+
day = defaultdict(int)
44
ans = 0
5-
for v in tasks:
5+
for task in tasks:
66
ans += 1
7-
ans = max(ans, mp.get(v, 0))
8-
mp[v] = ans + space + 1
7+
ans = max(ans, day[task])
8+
day[task] = ans + space + 1
99
return ans
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function taskSchedulerII(tasks: number[], space: number): number {
2+
const day = new Map<number, number>();
3+
let ans = 0;
4+
for (const task of tasks) {
5+
++ans;
6+
ans = Math.max(ans, day.get(task) ?? 0);
7+
day.set(task, ans + space + 1);
8+
}
9+
return ans;
10+
}

0 commit comments

Comments
 (0)