diff --git a/Array/TaskScheduler.swift b/Array/TaskScheduler.swift index fd110843..1ab3a7fd 100644 --- a/Array/TaskScheduler.swift +++ b/Array/TaskScheduler.swift @@ -1,6 +1,6 @@ /** * Question Link: https://leetcode.com/problems/task-scheduler/ - * Primary idea: Most frequent character should be put at head of each chunk and join the chunks with less frequent one. + * Primary idea: Most frequent character should be put at head of each cycle and join the chunks with less frequent one. * * Time Complexity: O(nlogn), Space Complexity: O(n) * @@ -16,6 +16,7 @@ let sortedTasks = taskFreqs.keys.sorted { return taskFreqs[$0]! > taskFreqs[$1]! } var mostFreqCount = 0 + // get interval number for last cycle for sortedTask in sortedTasks { if taskFreqs[sortedTask] != taskFreqs[sortedTasks[0]] { break @@ -24,6 +25,8 @@ mostFreqCount += 1 } + // when number of different tasks is greater than n + 1, and the most freqent task won't cause idle run, + // then we should return tasks.count return max(tasks.count, (taskFreqs[sortedTasks[0]]! - 1) * (n + 1) + mostFreqCount) } -} \ No newline at end of file +}