Open YuezhenQin opened 3 months ago
Usually, if a problem can be solved greedily, then it is the fastest way
to solve it. The hard part is figuring out if you can use greedy. The concept of "greedy" is extremely general and the main thing to practice is recognizing when it applies.
A
greedy algorithm
refers to a way of approaching problems that makes the locally optimal decision at every step.Let's break this definition down. First, what makes a decision
"optimal"
? This will depend on the problem. For example, if we are choosing some elements and the problem wants us to find the maximum sum of elements we take, then given a choice between two numbers, it is optimal to take the larger one.Second, what makes a decision
"local"
? A decision is local when it considers only the available options at the current step. It is based on the information it has at the time, and doesn't consider any consequences that may happen in the future from this decision.