Repository: TheAlgorithms/Java
Pull request #5398 (2024-08-25 22:36:30)

refactor: `LineSweep`
Class cleanup.
Readability improvements.
New tests added.
Tests refactored.


<!--
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/5398?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `88.88889%` with `1 line` in your changes missing coverage. Please review.
> Project coverage is 47.61%. Comparing base [(`7e9cdad`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/7e9cdad3ee205084822a46b45a21b2ed4467432e?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`da9a80f`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/da9a80f5cc2be2f816fd91ab7a24d8a612fdf0fc?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

| [Files](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5398?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [.../main/java/com/thealgorithms/others/LineSweep.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5398?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fothers%2FLineSweep.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9vdGhlcnMvTGluZVN3ZWVwLmphdmE=) | 88.88% | [0 Missing and 1 partial :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5398?src=pr&el=tree&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    #5398   +/-   ##
=========================================
  Coverage     47.60%   47.61%           
- Complexity     2944     2945    +1     
=========================================
  Files           524      524           
  Lines         15312    15313    +1     
  Branches       2906     2906           
=========================================
+ Hits           7290     7292    +2     
+ Misses         7720     7719    -1     
  Partials        302      302           
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5398?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/src/main/java/com/thealgorithms/others/LineSweep.java b/src/main/java/com/thealgorithms/others/LineSweep.java
index 946ba6ed..b7db964c 100644
--- a/src/main/java/com/thealgorithms/others/LineSweep.java
+++ b/src/main/java/com/thealgorithms/others/LineSweep.java
@@ -1,42 +1,49 @@
 package com.thealgorithms.others;
+
 import java.util.Arrays;
 import java.util.Comparator;
 
-/* Line Sweep algorithm can be used to solve range problems by first sorting the list of ranges
- * by the start value of the range in non-decreasing order and doing a "sweep" through the number
- * line(x-axis) by incrementing the start point by 1 and decrementing the end point+1 by 1 on the
- * number line.
- * An overlapping range is defined as (StartA <= EndB) AND (EndA >= StartB)
- * References
- * https://en.wikipedia.org/wiki/Sweep_line_algorithm
- * https://en.wikipedia.org/wiki/De_Morgan%27s_laws>
+/**
+ * The Line Sweep algorithm is used to solve range problems efficiently. It works by:
+ * 1. Sorting a list of ranges by their start values in non-decreasing order.
+ * 2. Sweeping through the number line (x-axis) while updating a count for each point based on the ranges.
+ *
+ * An overlapping range is defined as:
+ * - (StartA <= EndB) AND (EndA >= StartB)
+ *
+ * References:
+ * - https://en.wikipedia.org/wiki/Sweep_line_algorithm
+ * - https://en.wikipedia.org/wiki/De_Morgan%27s_laws
  */
 public final class LineSweep {
     private LineSweep() {
     }
 
     /**
-     * Find Maximum end point
-     *   param = ranges : Array of range[start,end]
-     *   return Maximum Endpoint
+     * Finds the maximum endpoint from a list of ranges.
+     *
+     * @param ranges a 2D array where each element is a range represented by [start, end]
+     * @return the maximum endpoint among all ranges
      */
     public static int findMaximumEndPoint(int[][] ranges) {
-        Arrays.sort(ranges, Comparator.comparingInt(a -> a[1]));
+        Arrays.sort(ranges, Comparator.comparingInt(range -> range[1]));
         return ranges[ranges.length - 1][1];
     }
 
     /**
-     * Find if any ranges overlap
-     *   param = ranges : Array of range[start,end]
-     *   return true if overlap exists false otherwise.
+     * Determines if any of the given ranges overlap.
+     *
+     * @param ranges a 2D array where each element is a range represented by [start, end]
+     * @return true if any ranges overlap, false otherwise
      */
     public static boolean isOverlap(int[][] ranges) {
+        if (ranges == null || ranges.length == 0) {
+            return false;
+        }
 
         int maximumEndPoint = findMaximumEndPoint(ranges);
-        Arrays.sort(ranges, Comparator.comparingInt(a -> a[0]));
         int[] numberLine = new int[maximumEndPoint + 2];
         for (int[] range : ranges) {
-
             int start = range[0];
             int end = range[1];
 
@@ -44,12 +51,12 @@ public final class LineSweep {
             numberLine[end + 1] -= 1;
         }
 
-        int current = 0;
-        int overlaps = 0;
-        for (int num : numberLine) {
-            current += num;
-            overlaps = Math.max(overlaps, current);
+        int currentCount = 0;
+        int maxOverlaps = 0;
+        for (int count : numberLine) {
+            currentCount += count;
+            maxOverlaps = Math.max(maxOverlaps, currentCount);
         }
-        return overlaps > 1;
+        return maxOverlaps > 1;
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/others/LineSweepTest.java b/src/test/java/com/thealgorithms/others/LineSweepTest.java
index 6bf6ef5b..59fd0faf 100644
--- a/src/test/java/com/thealgorithms/others/LineSweepTest.java
+++ b/src/test/java/com/thealgorithms/others/LineSweepTest.java
@@ -1,30 +1,37 @@
 package com.thealgorithms.others;
+
 import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertFalse;
-import static org.junit.jupiter.api.Assertions.assertTrue;
 
-import org.junit.jupiter.api.Test;
+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 LineSweepTest {
+    private record OverlapTestCase(int[][] ranges, boolean expected) {
+    }
 
-    @Test
-    void testForOverlap() {
-        int[][] arr = {{0, 10}, {7, 20}, {15, 24}};
-        assertTrue(LineSweep.isOverlap(arr));
+    private record MaximumEndPointTestCase(int[][] ranges, int expected) {
     }
 
-    @Test
-    void testForNoOverlap() {
-        int[][] arr = {{0, 10}, {11, 20}, {21, 24}};
-        assertFalse(LineSweep.isOverlap(arr));
+    @ParameterizedTest
+    @MethodSource("provideOverlapTestData")
+    void testIsOverlap(OverlapTestCase testCase) {
+        assertEquals(testCase.expected(), LineSweep.isOverlap(testCase.ranges()));
     }
-    @Test
-    void testForOverlapWhenEndAEqualsStartBAndViceVersa() {
-        int[][] arr = {{0, 10}, {10, 20}, {21, 24}};
-        assertTrue(LineSweep.isOverlap(arr));
+
+    private static Stream<Arguments> provideOverlapTestData() {
+        return Stream.of(Arguments.of(new OverlapTestCase(new int[][] {{0, 10}, {7, 20}, {15, 24}}, true)), Arguments.of(new OverlapTestCase(new int[][] {{0, 10}, {11, 20}, {21, 24}}, false)), Arguments.of(new OverlapTestCase(new int[][] {{0, 10}, {10, 20}, {21, 24}}, true)),
+            Arguments.of(new OverlapTestCase(new int[][] {{5, 10}}, false)), Arguments.of(new OverlapTestCase(new int[][] {{1, 5}, {1, 5}, {1, 5}}, true)), Arguments.of(new OverlapTestCase(new int[][] {{1, 1}, {2, 2}, {3, 3}}, false)), Arguments.of(new OverlapTestCase(new int[][] {}, false)));
     }
-    @Test
-    void testForMaximumEndPoint() {
-        int[][] arr = {{10, 20}, {1, 100}, {14, 16}, {1, 8}};
-        assertEquals(100, LineSweep.findMaximumEndPoint(arr));
+
+    @ParameterizedTest
+    @MethodSource("provideMaximumEndPointTestData")
+    void testFindMaximumEndPoint(MaximumEndPointTestCase testCase) {
+        assertEquals(testCase.expected(), LineSweep.findMaximumEndPoint(testCase.ranges()));
+    }
+
+    private static Stream<Arguments> provideMaximumEndPointTestData() {
+        return Stream.of(Arguments.of(new MaximumEndPointTestCase(new int[][] {{10, 20}, {1, 100}, {14, 16}, {1, 8}}, 100)));
     }
 }