Contents

Ant Army (with. Java)

   Jan 4, 2024     2 min read

This is a post about the “ant army” problem.

We’re going to learn by solving coding test problems, reflecting on the problems we solved, and exploring other ways to solve them.

Let’s start with the problem.

Problem

An army of ants is about to go hunting.

The ant army wants to take an army that is exactly the right size for the health of the prey.

General ants have an attack power of 5, soldier ants have an attack power of 3, and worker ants have an attack power of 1.

For example, if you want to hunt a Yeochi with 23 hit points, you can take 23 worker ants, but you can do so with fewer troops if you take four warlord ants and one trooper ant.

Complete the SOLUTION function to return how many ants you need to form the smallest possible army to match your prey’s HP, given the prey’s HP as a parameter.

Limitations

hp is a natural number.

0 ≤ hp ≤ 1000

Example input and output

hpresult
235
246
999201

My solution to the ### problem

class Solution {
    public int solution(int hp) {
        int answer = 0;
        int quo = 0;
        int rem = hp;
        for(int i = 5; i >= 1; i -= 2){
            quo = rem / i;
            rem = rem % i;
            if(quo != 0){
                answer += quo;
            }
        }
    } return answer;
}

Solution

Input: HP - the amount of health that needs to be restored.

Output: Number of times recovered using the healing item.

Utilizing Greedy Algorithm: The implementation uses a greedy algorithm to use healing items in the largest possible increments.

Introduction to the functions used:

REM / I and REM % I: division operations to calculate the number of times a healing item was used (QUO) and the remaining health (REM).

What could be improved:

Use of constants: In the current code, 5 and 2 are used as constants. You can replace these constants with variables for more flexibility.

Efficiency: The code is checking and attempting to heal sequentially based on the type of healing item given. This allows you to consider how to find the optimal healing scenario.

Input exception handling: You may need exception handling for when negative or invalid values are entered.

We can consider these improvements to make our code more complete.