Skip to content

Commit

Permalink
Add MinimumWaitingTime algorithm (TheAlgorithms#5794)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hardvan authored Oct 16, 2024
1 parent a537654 commit dfff8d9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@
* [JobSequencing](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/JobSequencing.java)
* [MergeIntervals](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/MergeIntervals.java)
* [MinimizingLateness](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/MinimizingLateness.java)
* [MinimumWaitingTime](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/MinimumWaitingTime.java)
* [StockProfitCalculator](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/StockProfitCalculator.java)
* io
* [BufferedReader](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/io/BufferedReader.java)
Expand Down Expand Up @@ -921,6 +922,7 @@
* [JobSequencingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/JobSequencingTest.java)
* [MergeIntervalsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/MergeIntervalsTest.java)
* [MinimizingLatenessTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/MinimizingLatenessTest.java)
* [MinimumWaitingTimeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/MinimumWaitingTimeTest.java)
* [StockProfitCalculatorTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/StockProfitCalculatorTest.java)
* io
* [BufferedReaderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/io/BufferedReaderTest.java)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.thealgorithms.greedyalgorithms;

import java.util.Arrays;

/**
* The MinimumWaitingTime class provides a method to calculate the minimum
* waiting time for a list of queries using a greedy algorithm.
*
* @author Hardvan
*/
public final class MinimumWaitingTime {
private MinimumWaitingTime() {
}

/**
* Calculates the minimum waiting time for a list of queries.
* The function sorts the queries in non-decreasing order and then calculates
* the waiting time for each query based on its position in the sorted list.
*
* @param queries an array of integers representing the query times in picoseconds
* @return the minimum waiting time in picoseconds
*/
public static int minimumWaitingTime(int[] queries) {
int n = queries.length;
if (n <= 1) {
return 0;
}

Arrays.sort(queries);

int totalWaitingTime = 0;
for (int i = 0; i < n; i++) {
totalWaitingTime += queries[i] * (n - i - 1);
}
return totalWaitingTime;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.thealgorithms.greedyalgorithms;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class MinimumWaitingTimeTest {

@ParameterizedTest
@MethodSource("provideTestCases")
public void testMinimumWaitingTime(int[] queries, int expected) {
assertEquals(expected, MinimumWaitingTime.minimumWaitingTime(queries));
}

private static Stream<Arguments> provideTestCases() {
return Stream.of(Arguments.of(new int[] {3, 2, 1, 2, 6}, 17), Arguments.of(new int[] {3, 2, 1}, 4), Arguments.of(new int[] {1, 2, 3, 4}, 10), Arguments.of(new int[] {5, 5, 5, 5}, 30), Arguments.of(new int[] {}, 0));
}
}

0 comments on commit dfff8d9

Please sign in to comment.