★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10470897.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks. Tasks could be done without original order. Each task could be done in one interval. For each interval, CPU could finish one task or just be idle.
However, there is a non-negative cooling interval n that means between two same tasks, there must be at least n intervals that CPU are doing different tasks or just be idle.
You need to return the least number of intervals the CPU will take to finish all the given tasks.
Example:
Input: tasks = ["A","A","A","B","B","B"], n = 2 Output: 8 Explanation: A -> B -> idle -> A -> B -> idle -> A -> B.
Note:
- The number of tasks is in the range [1, 10000].
- The integer n is in the range [0, 100].
给定一个用字符数组表示的 CPU 需要执行的任务列表。其中包含使用大写的 A - Z 字母表示的26 种不同种类的任务。任务可以以任意顺序执行,并且每个任务都可以在 1 个单位时间内执行完。CPU 在任何一个单位时间内都可以执行一个任务,或者在待命状态。
然而,两个相同种类的任务之间必须有长度为 n 的冷却时间,因此至少有连续 n 个单位时间内 CPU 在执行不同的任务,或者在待命状态。
你需要计算完成所有任务所需要的最短时间。
示例 1:
输入: tasks = ["A","A","A","B","B","B"], n = 2 输出: 8 执行顺序: A -> B -> (待命) -> A -> B -> (待命) -> A -> B.
注:
- 任务的总个数为 [1, 10000]。
- n 的取值范围为 [0, 100]。
1 class Solution { 2 func leastInterval(_ tasks: [Character], _ n: Int) -> Int { 3 var cnt:[Int] = [Int](repeating:0,count:26) 4 for task in tasks 5 { 6 //A:65 7 cnt[task.ascii - 65] += 1 8 } 9 cnt.sort() 10 var i:Int = 25 11 var mx:Int = cnt[25] 12 var len:Int = tasks.count 13 while (i >= 0 && cnt[i] == mx) 14 { 15 i -= 1 16 } 17 return max(len, (mx - 1) * (n + 1) + 25 - i) 18 } 19 } 20 21 //Character扩展 22 extension Character 23 { 24 //转ASCII整数值(定义小写为整数值) 25 var ascii: Int { 26 get { 27 return Int(self.unicodeScalars.first!.value) 28 } 29 } 30 }
1118ms
1 class Solution { 2 func leastInterval(_ tasks: [Character], _ n: Int) -> Int { 3 guard !tasks.isEmpty else {return 0} 4 var map = [Int](repeating: 0, count: 26) 5 let taskStr = String(tasks) 6 let offset = "A".utf8.first! 7 for task in taskStr.utf8{ 8 let count = Int(task - offset) 9 map[count] += 1 10 } 11 var mostFrequency = 0 12 var numOfMostFrequency = 0 13 for count in map { 14 if count == mostFrequency { 15 numOfMostFrequency += 1 16 } else if count > mostFrequency { 17 mostFrequency = count 18 numOfMostFrequency = 1 19 } 20 } 21 22 return max((n+1) * (mostFrequency-1) + numOfMostFrequency, tasks.count) 23 } 24 }
1208ms
1 class Solution { 2 func leastInterval(_ tasks: [Character], _ n: Int) -> Int { 3 guard n > 0 else { return tasks.count } 4 5 var items = Array<Int>(repeating: 0, count: 26) 6 let aValue = Int(UnicodeScalar("A").value) 7 tasks.forEach { (char) in 8 items[Int(UnicodeScalar(String(char))!.value) - aValue] += 1 9 } 10 11 items.sort() 12 var maxCount = items[25] - 1 13 var maxIdleTimes = (items[25] - 1) * n 14 15 for i in 1..<items.count { 16 guard items[25 - i] > 0 else { continue } 17 maxIdleTimes -= min(maxCount, items[25 - i]) 18 } 19 20 return maxIdleTimes < 0 ? tasks.count : tasks.count + maxIdleTimes 21 } 22 }
1224ms
1 class Solution { 2 func leastInterval(_ tasks: [Character], _ n: Int) -> Int { 3 var counter = [Character: Int]() 4 tasks.forEach{ counter[$0, default: 0] += 1 } 5 let m = counter.values.max()! 6 let numMax = counter.values.filter{ $0 == m }.count 7 let slots = (m - 1) * (n + 1) 8 return numMax + max(slots, tasks.count - numMax) 9 } 10 }
1240ms
1 class Solution { 2 func leastInterval(_ tasks: [Character], _ n: Int) -> Int { 3 var counts = [Character:Int]() 4 for task in tasks { 5 counts[task, default: 0] += 1 6 } 7 var keys = counts.keys.sorted { 8 return counts[$0]! > counts[$1]! 9 } 10 let maxCount = counts[keys.first!]! 11 keys.removeFirst() 12 var idleSlots = (maxCount - 1) * n 13 for char in keys { 14 idleSlots -= min(maxCount - 1, counts[char]!) 15 idleSlots = max(idleSlots, 0) 16 } 17 return idleSlots + tasks.count 18 } 19 }
1252ms
1 class Solution { 2 func leastInterval(_ tasks: [Character], _ n: Int) -> Int { 3 let totalTask = tasks.count 4 guard totalTask > 0 else { return 0 } 5 guard n > 0 else { return totalTask } 6 var taskCount: [Character: Int] = [:] 7 var biggestTaskCount = 1 8 for task in tasks { 9 if let count = taskCount[task] { 10 taskCount[task] = count + 1 11 if biggestTaskCount < count + 1 { 12 biggestTaskCount = count + 1 13 } 14 }else { 15 taskCount[task] = 1 16 } 17 } 18 19 if (biggestTaskCount * (n + 1)) <= tasks.count { 20 return tasks.count 21 } 22 23 24 var total = (biggestTaskCount - 1) * (n + 1) 25 26 for (_, value) in taskCount { 27 if value == biggestTaskCount { 28 total += 1 29 } 30 } 31 32 return total 33 } 34 }
1256ms
1 class Solution { 2 func leastInterval(_ tasks: [Character], _ n: Int) -> Int { 3 var map = [Character: Int]() 4 var maxCount = 0 5 for task in tasks { 6 if let val = map[task] { 7 map[task] = val + 1 8 maxCount = max(maxCount, val + 1) 9 } else { 10 maxCount = max(maxCount, 1) 11 map[task] = 1 12 } 13 } 14 15 let sortedMap = map.sorted { $0.1 > $1.1 } 16 maxCount -= 1 17 var idlesAmount = maxCount * n 18 var charCount = maxCount 19 for i in 1..<sortedMap.count { 20 let (key, val) = sortedMap[i] 21 idlesAmount -= min(val, maxCount) 22 } 23 24 return idlesAmount > 0 ? tasks.count + idlesAmount : tasks.count 25 } 26 }
1260ms
1 class Solution { 2 func leastInterval(_ tasks: [Character], _ n: Int) -> Int { 3 var countMap: [Character: Int] = [:] 4 for task in tasks { 5 countMap[task] = (countMap[task] ?? 0) + 1 6 } 7 let maxCount: Int = countMap.values.max() ?? 0 8 let fullPeriodCount = maxCount - 1 9 var maxIdles = fullPeriodCount * (n + 1) 10 for count in countMap.values { 11 maxIdles -= min(fullPeriodCount, count) 12 } 13 return tasks.count + max(0, maxIdles) 14 } 15 }
1264ms
1 class Solution { 2 func isValid(_ intervals: Int, _ table: [Character: Int], _ len: Int, _ maxCount: Int, _ maxCountTies: Int) -> Bool { 3 let rest = intervals%len 4 var rounds = intervals/len + (rest == 0 ? 0 : 1) 5 if rounds < maxCount || (rounds == maxCount && 0 < rest && rest < maxCountTies) { 6 return false 7 } 8 9 return true 10 } 11 12 func binarySearch(_ low: Int, _ high: Int, _ table: [Character : Int], _ n: Int, _ maxCount: Int, _ maxCountTies: Int) -> Int { 13 if low == high { 14 return low 15 } 16 17 let mid = (low+high)/2 18 if isValid(mid, table, n, maxCount, maxCountTies) == true { 19 return binarySearch(low, mid, table, n, maxCount, maxCountTies) 20 } else { 21 return binarySearch(mid+1, high, table, n, maxCount, maxCountTies) 22 } 23 24 } 25 26 func leastInterval(_ tasks: [Character], _ n: Int) -> Int { 27 if tasks.count == 0 { 28 return 0 29 } 30 31 if n == 0 { 32 return tasks.count 33 } 34 35 var table: [Character: Int] = [:] 36 for ch in tasks { 37 table[ch] = (table[ch] ?? 0) + 1 38 } 39 40 var maxCount = table.values.reduce(0, {max($0, $1)}) 41 var maxCountTies = table.values.reduce(0, {$0 + ($1 == maxCount ? 1 : 0)}) 42 43 let tasksCount = tasks.count 44 let low = max(tasksCount, (maxCount-1)*(n+1)+1) 45 let high = tasksCount*(n+1) 46 47 return binarySearch(low, high, table, n+1, maxCount, maxCountTies) 48 } 49 }
请发表评论