-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path621_TaskScheduler.swift
47 lines (43 loc) · 1.33 KB
/
621_TaskScheduler.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//
// 621_TaskScheduler.swift
// LeetcodeSwift
//
// Created by yansong li on 2018-04-20.
// Copyright © 2018 YANSONG LI. All rights reserved.
//
import Foundation
/**
Title: 621 Task Scheduler
URL: https://leetcode.com/problems/task-scheduler/
Space: O(1)
Time: O(N)
*/
class TaskScheduler {
func leastInterval(_ tasks: [Character], _ n: Int) -> Int {
var characterCounts: [Int] = Array(repeatElement(0, count: 26))
var maxCount: Int = 0
var maxAppearance: Int = 0
for cha in tasks {
let currentIndex = cha.unicodeScalarCodePoint() - "A".unicodeScalarCodePoint()
let currentCount = characterCounts[currentIndex]
characterCounts[currentIndex] = currentCount + 1
if characterCounts[currentIndex] > maxCount {
maxCount = characterCounts[currentIndex]
maxAppearance = 1
} else if characterCounts[currentIndex] == maxCount {
maxAppearance += 1
}
}
let taskCount = tasks.count
let occupiedCount = maxCount * maxAppearance
let emptySpotsCount = (maxCount - 1) * (n + 1 - maxAppearance)
let idleCounts = max(0, emptySpotsCount - (taskCount - occupiedCount))
return taskCount + idleCounts
}
}
extension String {
func unicodeScalarCodePoint() -> Int {
let scalars = self.unicodeScalars
return Int(scalars[scalars.startIndex].value)
}
}