Skip to content

Commit

Permalink
[Array] Add comment for Task Scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu authored Oct 5, 2019
1 parent 7906d80 commit f4365cc
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions Array/TaskScheduler.swift
Original file line number Diff line number Diff line change
@@ -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)
*
Expand All @@ -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
Expand All @@ -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)
}
}
}

0 comments on commit f4365cc

Please sign in to comment.