Java Coding Test Course, Making an Integer 1

It is very important to solve various algorithm problems in preparing for coding tests. In this course, we will develop basic problem-solving skills through the problem of ‘Making an Integer Equal to 1’ and look at the process of designing efficient algorithms. This problem asks for the minimum number of operations needed to turn a given integer into 1.

Problem Definition

Given an integer N, you can transform the number by choosing one of the following three operations:

  • Decrement: N - 1
  • Divide by 2: N / 2 (only possible if N is even)
  • Divide by 3: N / 3 (only possible if N is divisible by 3)

The goal is to find the minimum number of operations to make N equal to 1.

Example Input and Output

  • Input: N = 10
  • Output: 3 (10 → 9 → 3 → 1)

Approach to Solve the Problem

There are several approaches to solving this problem. Here we will introduce two methods: DFS (Depth-First Search) and DP (Dynamic Programming).

1. DFS (Depth-First Search)

First, we can consider using DFS to explore all possible paths. However, this approach can lead to a very high time complexity. For example, the number of possible paths can be quite large for N=10. Nevertheless, let’s approach it with DFS.

DFS Implementation Code

import java.util.HashMap;

public class Main {
    static int minSteps = Integer.MAX_VALUE;
    
    public static void main(String[] args) {
        int N = 10;
        findSteps(N, 0);
        System.out.println("Minimum steps to reach 1: " + minSteps);
    }
    
    private static void findSteps(int N, int steps) {
        if (N == 1) {
            minSteps = Math.min(minSteps, steps);
            return;
        }
        
        findSteps(N - 1, steps + 1); // Decrement
        if (N % 2 == 0) {
            findSteps(N / 2, steps + 1); // Divide by 2
        }
        if (N % 3 == 0) {
            findSteps(N / 3, steps + 1); // Divide by 3
        }
    }
}

The above code explores all possible paths from the given N. However, this method has a time complexity of O(3^N) due to duplicate calls and inefficient path exploration.

2. DP (Dynamic Programming)

Thus, a more efficient method is to use DP. By using DP, we can store previously computed results and reuse them when needed, reducing unnecessary calculations.

DP Implementation Code

public class Main {
    public static void main(String[] args) {
        int N = 10;
        System.out.println("Minimum steps to reach 1: " + minStepsDP(N));
    }

    private static int minStepsDP(int N) {
        int[] dp = new int[N + 1];
        dp[1] = 0; // Minimum operations to reach 1 is 0
        
        for (int i = 2; i <= N; i++) {
            dp[i] = dp[i - 1] + 1; // Decrement

            if (i % 2 == 0) {
                dp[i] = Math.min(dp[i], dp[i / 2] + 1); // Divide by 2
            }
            if (i % 3 == 0) {
                dp[i] = Math.min(dp[i], dp[i / 3] + 1); // Divide by 3
            }
        }
        
        return dp[N];
    }
}

The above DP implementation code uses an array dp to store the minimum steps needed to reach each number. The algorithm has a time complexity of O(N). It calculates the minimum steps by referencing the values of the previous numbers for each number.

Time Complexity Analysis

The DFS approach has a time complexity of O(3^N), making it very inefficient. In contrast, the DP approach has a time complexity of O(N), which allows for deriving the minimum steps for all numbers with at most one calculation.

Conclusion

In this course, we examined various approaches to making an integer equal to 1 and learned efficient problem-solving methods through DFS and DP. Understanding actual algorithms and developing the ability to solve problems based on them are important in preparing for coding tests. Practice learning various approaches to complex problems like this and choose optimized methods.

Practice Problems

Try to solve the additional problems below for practice:

  • For integer N = 15, find the minimum number of operations to make it 1.
  • For integer N = 25, find the minimum number of operations to make it 1.

As you solve the problems, try to output results for various input values. In coding tests, it is essential not just to solve the problem but to find the optimal solution.