Problems of greedy algorithm are intuitively confusing but surprisingly concise when riddle disclosed.
Description
The difficulty of this problem is understanding the pattern of how task and idle are placed.
Given a characters array tasks, representing the tasks a CPU needs to do, where each letter represents a different task. Tasks…
tasks
Problems of greedy algorithm are usually intuitively challenging. For this one, the high level concept is “while already able to jump (global), stay behind the accessible point and seek greedier choice (local)”.
Greedy algorithm: Builds up a solution piece by piece, always choose a local optimal solution (the way with the best result in current round), in the hope of leading to a global optimization.
A good practice for the greedy algorithm. To waive duplicate computation, keep one local observatory variable and update it along the iteration.