Java Coding Test Course – Greedy Algorithm
The Greedy Algorithm is a method of solving optimization problems by making the most optimal choice step by step. While the optimal choice at each step does not guarantee the optimal solution for the entire problem, it is often useful in many cases. In this article, we will solve a problem using the Greedy Algorithm.
Problem: Coin Change
Problem Description: Write a program to find the minimum number of coins needed to make a given amount. The types of coins are [500 won, 100 won, 50 won, 10 won].
For example, find the minimum number of coins needed to make 770 won.
Input
- Integer N (0 < N ≤ 10,000,000): Amount to be made
Output
- Integer K: Minimum number of coins needed
Input Example
770
Output Example
6
Problem Approach
To solve the problem, we apply the Greedy Algorithm by considering the types of coins given. The essence of the Greedy Algorithm is to use the coin with the highest value at each step. When the types of coins are sorted, we can continually reduce the remaining amount by using the highest coin first.
Code Implementation
Now, let’s implement the code to solve the problem using Java. Below is the Java code that solves the Coin Change problem.
import java.util.Scanner; public class CoinChange { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Input the given amount int N = scanner.nextInt(); // Types of coins to be used int[] coins = {500, 100, 50, 10}; int count = 0; // Variable to count the number of coins for (int coin : coins) { count += N / coin; // Add the number of coins that can be made with the current coin N %= coin; // Update the remaining amount } System.out.println(count); // Output the final number of coins scanner.close(); } }
Code Explanation
The code above finds the optimal solution through the following process:
- User Input: The Scanner class is used to receive the amount that the user wants to create.
- Pattern Setting: Define the array of coins to be used. The values of the coins are sorted in descending order.
- Coin Count Calculation: Iterate over each coin starting from the largest. Calculate how many can be made with the current coin and update the remaining amount.
- Result Output: Finally, the number of coins needed is outputted.
Time Complexity
The time complexity of this problem is O(1). The types of coins are fixed, and the result can be calculated in constant time regardless of the input amount.
Conclusion
The Greedy Algorithm is a method of solving problems by making the most optimal choice at each step. Through the Coin Change problem, we have learned the basic principles of the Greedy Algorithm and how to implement it in Java. This algorithm can be applied to various optimization problems and helps develop algorithmic thinking.
Further Reading
To gain a deeper understanding of the Greedy Algorithm, it is recommended to try other types of problems. For example, you can apply the Greedy Algorithm in problems such as Minimum Spanning Tree (MST) or Activity Selection Problems.
Appendix
The Greedy Algorithm can be effectively used to solve various problems, and eventually, you will challenge yourself with more complex optimization problems. In actual coding tests, it is important to read the problem carefully and determine whether the Greedy Algorithm is suitable. It is also beneficial to develop mathematical thinking and an understanding of data structures needed to solve problems together.