public class Solution { public int LeastInterval(char[] tasks, int n) { Dictionary<char, int> dic = new Dictionary<char, int>(); foreach (var c in tasks) { if (!dic.ContainsKey(c)) { dic.Add(c, 1); } else { dic[c]++; } } var x = dic.OrderByDescending(a => a.Value).FirstOrDefault().Value;//出现次数最多的任务的数量 var k = dic.Where(a => a.Value == x).Count(); return Math.Max(tasks.Length, (x - 1) * (n + 1) + k); } }
利用数学公式解题。