Skip to content

Commit 6403d1f

Browse files
aQuaaQua
aQua
authored and
aQua
committed
621 accepted, 19ms, the fast is 9ms
1 parent de52b3a commit 6403d1f

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

Algorithms/0621.task-scheduler/task-scheduler.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,31 @@ func leastInterval(tasks []byte, n int) int {
1212
sort.Ints(rec)
1313

1414
res := 0
15-
remain := 0
16-
begin := 0
15+
intervals := 0
16+
idx := 0
1717
for len(rec) > 0 {
18-
if remain > 0 {
19-
res += remain
18+
if intervals > 0 {
19+
res += intervals
2020
}
2121

22-
remain = n
22+
// 新周期
23+
intervals = n + 1
2324

24-
begin = len(rec) - 1
25-
for begin >= 0 && rec[begin] > 0 {
26-
rec[begin]--
27-
remain--
28-
begin--
25+
idx = len(rec) - 1
26+
for intervals > 0 && idx >= 0 && rec[idx] > 0 {
27+
rec[idx]--
28+
intervals--
29+
idx--
2930
res++
3031
}
3132

32-
rec = rec[begin+1:]
33+
sort.Ints(rec)
34+
idx = len(rec) - 1
35+
for idx >= 0 && rec[idx] > 0 {
36+
idx--
37+
}
38+
39+
rec = rec[idx+1:]
3340
}
3441

3542
return res

Algorithms/0621.task-scheduler/task-scheduler_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ func Test_Problem0621(t *testing.T) {
4747
12,
4848
},
4949
},
50+
51+
question{
52+
para{
53+
[]byte{'A', 'A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C'},
54+
100,
55+
},
56+
ans{
57+
506,
58+
},
59+
},
5060
// 如需多个测试,可以复制上方元素。
5161
}
5262

0 commit comments

Comments
 (0)