Skip to content

Commit edcfcae

Browse files
committed
feat: Add OptimalFileMerging new algorithm with Junit tests
1 parent 9c76b30 commit edcfcae

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.thealgorithms.greedyalgorithms;
2+
3+
import java.util.PriorityQueue;
4+
5+
/**
6+
* Class to solve the Optimal File Merging Problem.
7+
* The goal is to minimize the cost of merging files, where the cost of merging two files is the sum of their sizes.
8+
* The cost of merging all files is the sum of the costs of merging each pair of files.
9+
* Example:
10+
* files = [4, 3, 2, 6]
11+
* The minimum cost to merge all files is 29.
12+
* Steps:
13+
* 1. Merge files 2 and 3 (cost = 2 + 3 = 5). New files = [4, 5, 6]
14+
* 2. Merge files 4 and 5 (cost = 4 + 5 = 9). New files = [6, 9]
15+
* 3. Merge files 6 and 9 (cost = 6 + 9 = 15). New files = [15]
16+
* Total cost = 5 + 9 + 15 = 29
17+
*
18+
* @author Hardvan
19+
*/
20+
public final class OptimalFileMerging {
21+
private OptimalFileMerging() {
22+
}
23+
24+
/**
25+
* Calculates the minimum cost to merge all files.
26+
*
27+
* @param files array of file sizes
28+
* @return the minimum cost to merge the files
29+
*/
30+
public static int minMergeCost(int[] files) {
31+
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
32+
for (int file : files) {
33+
minHeap.add(file);
34+
}
35+
36+
int totalCost = 0;
37+
while (minHeap.size() > 1) {
38+
int first = minHeap.poll();
39+
int second = minHeap.poll();
40+
int cost = first + second;
41+
totalCost += cost;
42+
43+
minHeap.add(cost);
44+
}
45+
return totalCost;
46+
}
47+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.thealgorithms.greedyalgorithms;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
9+
10+
public class OptimalFileMergingTest {
11+
12+
@ParameterizedTest
13+
@MethodSource("fileMergingProvider")
14+
public void testMinMergeCost(int[] files, int expected) {
15+
assertEquals(expected, OptimalFileMerging.minMergeCost(files));
16+
}
17+
18+
private static Stream<Arguments> fileMergingProvider() {
19+
return Stream.of(Arguments.of(new int[] {4, 3, 2, 6}, 29), Arguments.of(new int[] {5}, 0), Arguments.of(new int[] {2, 2, 2}, 10), Arguments.of(new int[] {10, 5, 3, 2}, 35), Arguments.of(new int[] {1, 1, 1, 1}, 8), Arguments.of(new int[] {1, 2, 3, 4, 5}, 33),
20+
Arguments.of(new int[] {1, 2, 3, 4, 5, 6}, 51), Arguments.of(new int[] {1, 2, 3, 4, 5, 6, 7}, 74), Arguments.of(new int[] {1, 2, 3, 4, 5, 6, 7, 8}, 102), Arguments.of(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9}, 135));
21+
}
22+
}

0 commit comments

Comments
 (0)