# Greedy approach vs Dynamic programming

• Difficulty Level : Medium
• Last Updated : 12 Jan, 2023

## Greedy approach:

A Greedy algorithm is an algorithmic paradigm that builds up a solution piece by piece, always choosing the next piece that offers the most obvious and immediate benefit. So the problems where choosing locally optimal also leads to a global solution is the best fit for Greedy.

Example: In Fractional Knapsack Problem the local optimal strategy is to choose the item that has maximum value vs weight ratio. This strategy also leads to global optimal solution because we allowed taking fractions of an item.

### Characteristics of Greedy approach:

A problem that can be solved using the Greedy approach follows the below-mentioned properties:

• Optimal substructure property.
• Minimization or Maximization of quantity is required.
• Ordered data is available such as data on increasing profit, decreasing cost, etc.
• Non-overlapping subproblems.

Standard problems on Greedy Approach:

## Dynamic Programming:

Dynamic programming is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of subproblems so that we do not have to re-compute them when needed later. This simple optimization reduces time complexities from exponential to polynomial.

Example: If we write a simple recursive solution for Fibonacci Numbers, we get exponential time complexity and to optimize it by storing solutions of subproblems, time complexity reduces to linear this can be achieved by Tabulation or Memoization method of Dynamic programming.

### Characteristics of Dynamic Programming:

A problem that can be solved using Dynamic Programming must follow the below mentioned properties:

• Optimal substructure property.
• Overlapping subproblems.

Standard problems on Dynamic Programming:

### Below are some major differences between Greedy method and Dynamic programming:

Related Articles:

My Personal Notes arrow_drop_up