Greedy Algorithms
Last Updated :
07 Apr, 2025
Improve
Greedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution.
- At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get the next optimal choice quickly. We sometimes also use a priority queue to get the next optimal item.
- After making a choice, we check for constraints (if there are any) and keep picking until we find the solution.
- Greedy algorithms do not always give the best solution. For example, in coin change and 0/1 knapsack problems, we get the best solution using Dynamic Programming.
- Examples of popular algorithms where Greedy gives the best solution are Fractional Knapsack, Dijkstra's algorithm, Kruskal's algorithm, Huffman coding and Prim's Algorithm
Basics of Greedy Algorithm
Easy Problems on Greedy Algorithm
- Fractional Knapsack
- Min Cost to Make Array Size 1
- Min Rotations for Circular Lock
- Max Composite Numbers to Make n
- Smallest Subset Greater Sum
- Assign Cookies
- Buy Maximum Stocks
- Max Consecutive Diff Sum
- Min and Max Costs to buy all
- Min Notes with Given Sum
- Max Equal Sum of Three Stacks
Medium Problems on Greedy Algorithm
- Activity Selection Problem
- Jump Game
- Job Sequencing Problem
- Egyptian Fraction
- Merge Overlapping Intervals
- Min Fibonacci Terms with Sum K
- Minimum Platforms
- Min Cost to Connect n ropes
- Max trains
- Partition 1 to n into two min diff groups
- Paper cut into min squares
- Min diff groups of size two
- Max Satisfied Customers
- Min initial vertices to traverse matrix with constraints
- Largest palindromic number by permuting digits
- Smallest with n digits and digits sum
- Lexicographically largest subsequence with every char at least k times
Hard Problems on Greedy Algorithm
- Minimize the Max Height Diff
- Making max equal with k updates
- Minimize cash flow among friends
- Minimum Cost to cut a board into squares
- Minimum cost to process m tasks where switching costs
- Minimum time to finish all jobs with given constraints
- Minimize the maximum difference between the heights of towers
- Minimum edges to reverse to make path from a source to a destination
- Find the Largest Cube formed by Deleting minimum Digits from a number
- Rearrange characters in a string such that no two adjacent are same
- Rearrange a string so that all same characters become d distance away
Standard Greedy Algorithms
- Activity Selection Problem
- Job Sequencing Problem
- Huffman Coding
- Huffman Decoding
- Water Connection Problem
- Minimum Swaps for Bracket Balancing
- Egyptian Fraction
- Policemen catch thieves
- Fitting Shelves Problem
- Assign Mice to Holes
Greedy Problems on Array
- Minimum product subset of an array
- Maximize array sum after K negations using Sorting
- Minimum sum of product of two arrays
- Minimum sum of absolute difference of pairs of two arrays
- Minimum increment/decrement to make array non-Increasing
- Sorting array with reverse around middle
- Sum of Areas of Rectangles possible for an array
- Largest lexicographic array with at-most K consecutive swaps
- Partition into two subarrays of lengths k and (N – k) such that the difference of sums is maximum
Greedy Problems in Operating System
- First Fit algorithm in Memory Management
- Best Fit algorithm in Memory Management
- Worst Fit algorithm in Memory Management
- Shortest Job First Scheduling
- Job Scheduling with two jobs allowed at a time
- Program for Optimal Page Replacement Algorithm
Greedy Problems on Graph
- Kruskal’s Minimum Spanning Tree
- Prim’s Minimum Spanning Tree
- Boruvka’s Minimum Spanning Tree
- Dijkastra’s Shortest Path Algorithm
- Dial’s Algorithm
- Minimum cost to connect all cities
- Max Flow Problem Introduction
- Number of single cycle components in an undirected graph
Approximate Greedy Algorithm for NP Complete
- Set cover problem
- Bin Packing Problem
- Graph Coloring
- K-centers problem
- Shortest superstring problem
- Approximate solution for Travelling Salesman Problem using MST
Greedy for Special cases of DP
Quick Links
- Learn Data Structure and Algorithms | DSA Tutorial
- Top 20 Greedy Algorithms Interview Questions
- ‘Practice Problems’ on Greedy Algorithms
- ‘Quiz’ on Greedy Algorithms