File tree Expand file tree Collapse file tree 6 files changed +221
-2
lines changed
solution/0600-0699/0621.Task Scheduler Expand file tree Collapse file tree 6 files changed +221
-2
lines changed Original file line number Diff line number Diff line change 58
58
59
59
<!-- 这里可写通用的实现逻辑 -->
60
60
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
+
61
71
<!-- tabs:start -->
62
72
63
73
### ** Python3**
64
74
65
75
<!-- 这里可写当前语言的特殊实现逻辑 -->
66
76
67
77
``` 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)
69
84
```
70
85
71
86
### ** Java**
72
87
73
88
<!-- 这里可写当前语言的特殊实现逻辑 -->
74
89
75
90
``` 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
+ ```
76
132
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
+ }
77
160
```
78
161
79
162
### ** ...**
Original file line number Diff line number Diff line change @@ -60,13 +60,85 @@ A -> B -> C -> A -> D -> E -> A -> F -> G -> A ->
60
60
### ** Python3**
61
61
62
62
``` 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)
64
69
```
65
70
66
71
### ** Java**
67
72
68
73
``` 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
+ ```
69
115
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
+ }
70
142
```
71
143
72
144
### ** ...**
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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 )
You can’t perform that action at this time.
0 commit comments