Let us see with the help of below examples about how greedy algorithm can be used to find optimal solutions.
Consider you want to visit a restaurant (point B) in your car from your home (Point A) and there are 20 possible ‘paths’ or ‘routes’. The paths are composed of many roads - each adjacent to the other. Imagine that upon leaving your home, you would encounter many junctions since we have many roads splitting at many points.
Let us assume that these junctions don’t have markers which shows the remaining distance to restaurant. Instead of that, we know the length of all the individual roads from here. To make things simpler, assume that you know these lengths before you are leaving from your home. Naturally you would want to reach the restaurant using minimum fuel and as early as possible. Right?
So, if you choose to minimize cost of the fuel, it would be same as taking paths of minimum length. Now, how should we proceed?
Simple, isn’t it? You don’t need to calculate exhaustively all road lengths in advance (especially when you are hungry!). You just have to follow minimum path length road at a given junction. This reduces a lots of wasteful computations.
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Highest Product | 200 |
|
29:25 | |
Bulbs | 200 |
|
23:18 | |
Disjoint Intervals | 200 |
|
47:18 | |
Largest Permutation | 250 |
|
53:54 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Meeting rooms | 200 |
|
60:19 | |
Distribute Candy | 300 |
|
65:48 | |
Seats | 300 |
|
74:35 | |
Assign Mice to Holes | 300 |
|
21:24 | |
Majority Element | 400 |
|
19:07 | |
Gas Station | 700 |
|
56:42 |