Skip to content

Commit 723deaa

Browse files
committed
[Array] Add a solution to Task Scheduler
1 parent f486f65 commit 723deaa

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

Array/TaskScheduler.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
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.
4+
*
5+
* Time Complexity: O(nlogn), Space Complexity: O(n)
6+
*
7+
*/
8+
9+
class TaskScheduler {
10+
func leastInterval(_ tasks: [Character], _ n: Int) -> Int {
11+
guard tasks.count > 0 else {
12+
return 0
13+
}
14+
15+
let taskFreqs = Dictionary(tasks.map { ($0, 1) }, uniquingKeysWith: +)
16+
let sortedTasks = taskFreqs.keys.sorted { return taskFreqs[$0]! > taskFreqs[$1]! }
17+
var mostFreqCount = 0
18+
19+
for sortedTask in sortedTasks {
20+
if taskFreqs[sortedTask] != taskFreqs[sortedTasks[0]] {
21+
break
22+
}
23+
24+
mostFreqCount += 1
25+
}
26+
27+
return max(tasks.count, (taskFreqs[sortedTasks[0]]! - 1) * (n + 1) + mostFreqCount)
28+
}
29+
}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* [Microsoft](#microsoft)
2828

2929
## Progress
30-
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 231 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
30+
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 232 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
3131

3232

3333
## Array
@@ -67,6 +67,7 @@
6767
[Next Permutation](https://leetcode.com/problems/next-permutation/)| [Swift](./Array/NextPermutation.swift)| Medium| O(n)| O(1)|
6868
[Gas Station](https://leetcode.com/problems/gas-station/)| [Swift](./Array/GasStation.swift)| Medium| O(n)| O(1)|
6969
[Game of Life](https://leetcode.com/problems/game-of-life/)| [Swift](./Array/GameLife.swift)| Medium| O(n)| O(1)|
70+
[Task Scheduler](https://leetcode.com/problems/task-scheduler/)| [Swift](./Array/TaskScheduler.swift)| Medium| O(nlogn)| O(n)|
7071
[Sliding Window Maximum ](https://leetcode.com/problems/sliding-window-maximum/)| [Swift](./Array/SlidingWindowMaximum.swift)| Hard| O(n)| O(n)|
7172
[Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/)| [Swift](./Array/LongestConsecutiveSequence.swift)| Hard| O(n)| O(n)|
7273

0 commit comments

Comments
 (0)