1. Introduction
The Optimal File Merge problem asks us to merge multiple files into a single file with the minimum possible total computation cost.
Each file has a certain size. When two files are merged, the cost of the operation is the sum of their sizes.
For example, if we merge files of sizes 3 and 2:
Cost = 3 + 2 = 5
New file size = 5The newly created file can then be merged with another file.
The important question is:
In which order should we merge the files so that the total cost is minimum?
The answer is to always merge the two smallest files first.
To efficiently find the two smallest files, we use a Min Heap, implemented in Java using PriorityQueue.
2. Example
Consider:
files = [3, 2, 4]There are multiple ways to merge these files.
Approach 1
2 + 3 = 5
5 + 4 = 9
Total cost = 5 + 9 = 14Approach 2
3 + 4 = 7
7 + 2 = 9
Total cost = 7 + 9 = 16Therefore:
Minimum Cost = 14The first approach is better.
3. Important Observation
The main observation is:
Always merge the two smallest files first.
Why?
Suppose we merge a large file early.
That merged file becomes part of future merge operations, so its size contributes to the cost multiple times.
By merging smaller files first, we keep the intermediate file sizes as small as possible.
This is a Greedy Algorithm.
4. Why Do We Need a Min Heap?
At every step, we need to find:
Smallest file
Second smallest fileIf we search the array every time, it can become inefficient.
For example, if there are n files and we repeatedly search for the smallest elements, the complexity can become large.
A Min Heap solves this efficiently.
In Java:
PriorityQueue<Integer> pq = new PriorityQueue<>();By default, Java's PriorityQueue works as a Min Heap.
Therefore:
pq.poll()always returns the smallest element.
5. Algorithm
The algorithm is simple:
Step 1
Create a Min Heap.
Step 2
Insert all file sizes into the heap.
Step 3
While there is more than one file:
Remove the smallest file.
Remove the second smallest file.
Add their sizes.
Add the merged size to the total cost.
Insert the merged file back into the heap.
Step 4
Return the total cost.
6. Java Code
import java.util.PriorityQueue;
class Solution {
public int minComputation(int[] files) {
// Create a Min Heap
PriorityQueue<Integer> pq = new PriorityQueue<>();
// Add all file sizes to the heap
for (int file : files) {
pq.add(file);
}
int totalCost = 0;
// Continue until only one file remains
while (pq.size() > 1) {
// Get the two smallest files
int x = pq.poll();
int y = pq.poll();
// Merge the two files
int merged = x + y;
// Add merge cost
totalCost += merged;
// Add the merged file back to the heap
pq.add(merged);
}
return totalCost;
}
}7. Code Explanation
Let's understand each part of the code.
Creating the Min Heap
PriorityQueue<Integer> pq = new PriorityQueue<>();Java's PriorityQueue automatically keeps the smallest element at the top.
For example:
Input:
[3, 2, 4]
Min Heap:
2
3
4So:
pq.poll();returns:
2Adding Files
for (int file : files) {
pq.add(file);
}Every file size is inserted into the priority queue.
For:
files = [2, 3, 6, 5, 7, 4]The heap contains all six values.
The important point is that we don't need to manually sort the array.

Join the conversation! Your thoughts help the community grow.