Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5c8465f

Browse files
refactor 757
1 parent73f3431 commit5c8465f

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
importjava.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
publicclass_757 {
20-
publicstaticclassSolution{
14+
publicstaticclassSolution{
2115
publicintintersectionSizeTwo(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
intcount =0;
24-
intstartTime =Integer.MIN_VALUE,endTime =Integer.MIN_VALUE;
18+
intstartTime =Integer.MIN_VALUE;
19+
intendTime =Integer.MIN_VALUE;
2520

26-
for(intinterval[] :intervals){
27-
if(startTime >=interval[0])
21+
for(intinterval[] :intervals){
22+
if(startTime >=interval[0]) {
2823
continue;
29-
elseif(endTime >=interval[0]){
24+
}elseif(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
intintervals[][];
1212

1313
@BeforeClass
14-
publicstaticvoidsetup(){
14+
publicstaticvoidsetup(){
1515
solution =new_757.Solution();
1616
}
17+
1718
@Test
1819
publicvoidtest1() {
19-
intervals =newint[][]{{1,3},{1,4}, {2,5},{3,5}};
20+
intervals =newint[][]{{1,3},{1,4}, {2,5},{3,5}};
2021
assertEquals(3,solution.intersectionSizeTwo(intervals));
2122
}
23+
2224
@Test
2325
publicvoidtest2() {
24-
intervals =newint[][]{{16,18},{11,18}, {15,23},{1,16}, {10,16},{6,19}, {18,20}, {7,19}};
26+
intervals =newint[][]{{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)

[8]ページ先頭

©2009-2025 Movatter.jp