File tree 1 file changed +5
-2
lines changed
1 file changed +5
-2
lines changed Original file line number Diff line number Diff line change 1
1
/**
2
2
* Question Link: https://leetcode.com/problems/task-scheduler/
3
- * Primary idea: Most frequent character should be put at head of each chunk and join the chunks with less frequent one.
3
+ * Primary idea: Most frequent character should be put at head of each cycle and join the chunks with less frequent one.
4
4
*
5
5
* Time Complexity: O(nlogn), Space Complexity: O(n)
6
6
*
16
16
let sortedTasks = taskFreqs. keys. sorted { return taskFreqs [ $0] ! > taskFreqs [ $1] ! }
17
17
var mostFreqCount = 0
18
18
19
+ // get interval number for last cycle
19
20
for sortedTask in sortedTasks {
20
21
if taskFreqs [ sortedTask] != taskFreqs [ sortedTasks [ 0 ] ] {
21
22
break
24
25
mostFreqCount += 1
25
26
}
26
27
28
+ // when number of different tasks is greater than n + 1, and the most freqent task won't cause idle run,
29
+ // then we should return tasks.count
27
30
return max ( tasks. count, ( taskFreqs [ sortedTasks [ 0 ] ] ! - 1 ) * ( n + 1 ) + mostFreqCount)
28
31
}
29
- }
32
+ }
You can’t perform that action at this time.
0 commit comments