Using greedy algorithms to solve problems
A greedy algorithm works by finding the optimal solution at every stage. So before processing the next step, it will decide its next step based on the previous outcome and the current needs of the application. In this way, it is better than dynamic programming. However, we cannot apply this principle to all problems, hence a greedy algorithm cannot be used for all situations.
Getting ready
To go through this recipe, you will need a machine running Windows. You also need to have a working copy of Visual Studio installed on your Windows machine. No other prerequisites are required.
How to do it…
In this recipe, we will find out how easy it is to use greedy algorithm to solve a problem:
Open Visual Studio.
Create a new C++ project.
Select Win32 Console Application.
Add the
Source.cpp
files.Add the following lines of code to it:
#include <iostream> #include <conio.h> using namespace std; void printMaxActivities(int start_Time[], int finish_Time...