@dwyanecf

I guess a more clear explanation for "* (n + 1)" is each chunk has at lease n + 1 chars when the frame is formed at first, not necessarily "for the for loop" as you said.

Assume the most frequent letter(s) is with count k, then there are two cases:

When the whole frame is "loose" -- when there is still unused idle time in each chunk, result = (c[25] - 1) * (n + 1) + 25 - i where "25 - i" simply counts how many letters are with count k and n + 1 is the length of each chunk. Since we have k (in this case k == c[25]) chunks, the total length of the first k - 1 chunks == (k - 1) * (n + 1). For the last chunk, we don't need the "whole chunk" (i.e., if the chunk is "ABXXX", we only will need the "AB". Because there are no more "less frequent letters" left so we can remove the last three spaces)

When the frame is "dense" (fully filled), the length of each chunk > n + 1. In this case it could be: a) initially the number of letters with count k is > n + 1 so the frame is already "fully filled without space left", or 2) the length of each chunk is > n + 1 after insertion. We still calculate the total length by adding up all the chunks, and it will == the length of the task at last.

Here is my Python version:

'''

def leastInterval(self, tasks, n):
c = [0] * 26
for task in tasks:
c[ord(task) - ord('A')] += 1
c.sort()
i = 25
while i >= 0 and c[i] == c[25]:
i -= 1
return max(len(tasks), (c[25] - 1) * (n + 1) + 25 - i)

'''