Repository: TheAlgorithms/Java
Pull request #5794 (2024-10-14 04:06:34)

feat: Add `MinimumWaitingTime` new algorithm with Junit tests
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5794?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 64.23%. Comparing base [(`213fd5a`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/213fd5a4931ba1cb74fed76ff97043ae34ff174b?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`6c13878`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/6c138781f8e0832a58780514d293e20748043504?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5794      +/-   ##
============================================
+ Coverage     64.21%   64.23%   +0.02%     
- Complexity     4216     4220       +4     
============================================
  Files           588      589       +1     
  Lines         16426    16434       +8     
  Branches       3170     3172       +2     
============================================
+ Hits          10548    10557       +9     
  Misses         5448     5448              
+ Partials        430      429       -1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5794?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/DIRECTORY.md b/DIRECTORY.md
index aa34bf1a..e7a08a12 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -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)
@@ -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)
diff --git a/src/main/java/com/thealgorithms/greedyalgorithms/MinimumWaitingTime.java b/src/main/java/com/thealgorithms/greedyalgorithms/MinimumWaitingTime.java
new file mode 100644
index 00000000..2341bcde
--- /dev/null
+++ b/src/main/java/com/thealgorithms/greedyalgorithms/MinimumWaitingTime.java
@@ -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;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/greedyalgorithms/MinimumWaitingTimeTest.java b/src/test/java/com/thealgorithms/greedyalgorithms/MinimumWaitingTimeTest.java
new file mode 100644
index 00000000..64cb4b80
--- /dev/null
+++ b/src/test/java/com/thealgorithms/greedyalgorithms/MinimumWaitingTimeTest.java
@@ -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));
+    }
+}