Greedy Algorithm



John Rankin @ May 10, 2023

The Greedy Algorithm is a problem-solving technique that follows the heuristic of making locally optimal choices at each step with the hope of finding a global optimum solution. It is commonly used for optimization problems where you need to find the best solution among a set of choices.

Here's an example of how you can use the Greedy Algorithm pattern to solve a coding problem:

Problem: Given a set of coins of different denominations and a target amount, find the minimum number of coins needed to make up that amount. Assume you have an infinite supply of each coin denomination.

Example:
Input: coins = [1, 5, 10, 25],
amount = 36
Output: 3
Explanation: The minimum number of coins needed to make 36 cents is 3 (1 coin of 25 cents, 1 coin of 10 cents, and 1 coin of 1 cent).

Solution: To solve this problem using the Greedy Algorithm, we can follow these steps:

  1. Sort the coins array in descending order to start with the highest denomination first.
  2. Initialize a variable called "count" to keep track of the number of coins used.
  3. Iterate over the coins array:
    • While the current coin denomination is less than or equal to the remaining amount, subtract the coin value from the amount and increment the count.
    • Repeat this step until the remaining amount becomes 0 or no more coins can be used.
  4. Finally, return the count, which represents the minimum number of coins needed.

Here's the implementation in Python:

def coinChange(coins, amount):
    coins.sort(reverse=True)
    count = 0
    for coin in coins:
        while amount >= coin:
            amount -= coin
            count += 1
    if amount > 0:
        return -1  # If it's not possible to make up the amount with given coins
    return count

# Test the function
coins = [1, 5, 10, 25]
amount = 36
print(coinChange(coins, amount))  # Output: 3

In this example, the Greedy Algorithm works because at each step, we select the largest coin denomination that is less than or equal to the remaining amount. By doing so, we minimize the number of coins used to reach the target amount.

Note that the Greedy Algorithm doesn't always guarantee an optimal solution for every problem. It's important to analyze the problem's constraints and ensure that the greedy approach will lead to the correct answer



 

 

 

Most Recent Posts


Bitwise XOR-exclusive OR

Read This
May 10, 2023

0-1 Knapsack

Read This
May 10, 2023

K-Way Merge

Read This
May 10, 2023

Subsets

Read This
May 10, 2023

Backtracking

Read This
May 10, 2023

Greedy Algorithm

Read This
May 10, 2023

Trie

Read This
May 10, 2023

Depth-First Search-DFS

Read This
May 10, 2023

Breadth-First Search-BFS

Read This
May 10, 2023

Binary Search Tree-BST

Read This
May 10, 2023

Top K Elements

Read This
May 10, 2023

Binary Search Pattern

Read This
May 10, 2023

Cyclic Sort pattern

Read This
May 10, 2023

Merge Intervals

Read This
May 10, 2023

Day 7 - DynamoDB - and Working with 2 Services - Lambda

Read This
August 25, 2018

Day 6 - Lambda - Creating an API

Read This
August 23, 2018

AWS - Day 5 - S3 - Simple Storage Service

Read This
August 22, 2018

AWS - Day 4 - AWS CLI Useful Scripts

Read This
August 21, 2018

AWS - Day 3 - Create Container from another container

Read This
August 20, 2018

Day 2 - Docker Intro

Read This
August 17, 2018

AWS - Day1 - Tutorial - Launching my first Docker Container

Read This
August 16, 2018

AWS - Day 1 - Signing up and testing out their tutorials

Read This
August 16, 2018

Dynamic Programming - Edit Distance

Read This
December 19, 2016

Dynamic Programming - Fibonacci

Read This
December 19, 2016