Skip to content

Commit 5c8465f

Browse files
refactor 757
1 parent 73f3431 commit 5c8465f

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

src/main/java/com/fishercoder/solutions/_757.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22

33
import java.util.Arrays;
44

5-
/**
6-
* Problem 757: Set Intersection Size At Least Two
7-
* An integer interval [a, b] (for integers a < b) is a set of all consecutive integers from a to b, including a and b.
8-
* Find the minimum size of a set S such that for every integer interval A in intervals, the intersection of S with A has size at least 2.
9-
*/
10-
115
/**
126
* Approach: Sort the intervals in the ascending order of end range.
137
* In case if the end range of any 2 intervals match,
@@ -17,21 +11,21 @@
1711
* The reason for sorting based on descending order of start range is to get minimum possible size of S that intersect with A of atleast size 2
1812
*/
1913
public class _757 {
20-
public static class Solution{
14+
public static class Solution {
2115
public int intersectionSizeTwo(int[][] intervals) {
22-
Arrays.sort(intervals, (a, b) -> a[1] == b[1] ? b[0] - a[0]: a[1] - b[1]);
16+
Arrays.sort(intervals, (a, b) -> a[1] == b[1] ? b[0] - a[0] : a[1] - b[1]);
2317
int count = 0;
24-
int startTime = Integer.MIN_VALUE, endTime = Integer.MIN_VALUE;
18+
int startTime = Integer.MIN_VALUE;
19+
int endTime = Integer.MIN_VALUE;
2520

26-
for(int interval[] : intervals){
27-
if(startTime >= interval[0])
21+
for (int interval[] : intervals) {
22+
if (startTime >= interval[0]) {
2823
continue;
29-
else if(endTime >= interval[0]){
24+
} else if (endTime >= interval[0]) {
3025
startTime = endTime;
3126
endTime = interval[1];
3227
count += 1;
33-
}
34-
else{
28+
} else {
3529
startTime = interval[1] - 1;
3630
endTime = interval[1];
3731
count += 2;

src/test/java/com/fishercoder/_757Test.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,19 @@ public class _757Test {
1111
int intervals[][];
1212

1313
@BeforeClass
14-
public static void setup(){
14+
public static void setup() {
1515
solution = new _757.Solution();
1616
}
17+
1718
@Test
1819
public void test1() {
19-
intervals = new int [][]{{1,3},{1, 4}, {2,5},{3,5}};
20+
intervals = new int[][]{{1, 3}, {1, 4}, {2, 5}, {3, 5}};
2021
assertEquals(3, solution.intersectionSizeTwo(intervals));
2122
}
23+
2224
@Test
2325
public void test2() {
24-
intervals = new int [][]{{16,18},{11, 18}, {15,23},{1,16}, {10,16},{6,19}, {18,20}, {7,19}};
26+
intervals = new int[][]{{16, 18}, {11, 18}, {15, 23}, {1, 16}, {10, 16}, {6, 19}, {18, 20}, {7, 19}};
2527
assertEquals(4, solution.intersectionSizeTwo(intervals));
2628
}
2729
}

0 commit comments

Comments
 (0)