Intuitive: construct string from left to right. Use priority queue to firstly consume most frequent char. When a char is used, it has to wait k time to join the pool again. O(Nlog26) time -> O(N).
0358. Rearrange String k Distance Apartarrow-up-right
Visualization: get most frequent char, fill each slot, e.g. A B ? ? A B ? ? A B
A B ? ? A B ? ? A B
0621. Task Schedulerarrow-up-right
0767. Reorganize Stringarrow-up-right
Last updated 4 years ago