# Greedy

> Disclaimer: I put Greedy algorithm in the Foundemental section just because it is a topic that is fairly straightforward and can be introduced early in the CP learning journey. The depth and brilliance of Greedy algo is more and beyond advanced (especially the math proof behind).

A **greedy algorithm** constructs a solution to the problem by always making a choice that looks the best **at the moment.** A greedy algorithm never takes back its choices, but directly constructs the final solution. For this reason, greedy algorithms are usually very efficient.&#x20;

**Locally optimal decision leads to globally optimal results.** A careful mathematical and logical proof can be usually conducted to show if a problem can be solved by a greedy algorithm.

**That said, it is common and tempting for one to design a greedy solution that does not solve the problem.**&#x20;

## Sorting

Sorting is usually needed for many greedy problems because it establishes a natural optimal solution for the min/max element in an array at every decision point.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://welchj.gitbook.io/cp-notes/foundementals/greedy.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
