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

Commit73f3431

Browse files
authored
Add solution for 757 (fishercoder1534#124)
* added solution for 757* added test cases for 757* updated readme* update readme
1 parentd3ce7de commit73f3431

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ _If you like this project, please leave me a star._ ★
361361
|762|[Prime Number of Set Bits in Binary Representation](https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_762.java)||Easy|
362362
|760|[Find Anagram Mappings](https://leetcode.com/problems/find-anagram-mappings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_760.java)||Easy|
363363
|758|[Bold Words in String](https://leetcode.com/problems/bold-words-in-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_758.java)||Easy|
364+
|757|[Set Intersection Size At Least Two](https://leetcode.com/problems/set-intersection-size-at-least-two/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_757.java)||Hard|
364365
|756|[Pyramid Transition Matrix](https://leetcode.com/problems/pyramid-transition-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_756.java) | |Medium| Backtracking
365366
|755|[Pour Water](https://leetcode.com/problems/pour-water/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_755.java) ||Medium| Array
366367
|754|[Reach a Number](https://leetcode.com/problems/reach-a-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_754.java) ||Medium| Math
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.Arrays;
4+
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+
11+
/**
12+
* Approach: Sort the intervals in the ascending order of end range.
13+
* In case if the end range of any 2 intervals match,
14+
* sort those intervals based on the descending order of start range
15+
* e.g. intervals = [[1, 3], [1, 4], [2, 5], [3, 5]]
16+
* After sorting, intervals[] becomes = [[1,3], [1,4], [3,5],[2,5]]
17+
* 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
18+
*/
19+
publicclass_757 {
20+
publicstaticclassSolution{
21+
publicintintersectionSizeTwo(int[][]intervals) {
22+
Arrays.sort(intervals, (a,b) ->a[1] ==b[1] ?b[0] -a[0]:a[1] -b[1]);
23+
intcount =0;
24+
intstartTime =Integer.MIN_VALUE,endTime =Integer.MIN_VALUE;
25+
26+
for(intinterval[] :intervals){
27+
if(startTime >=interval[0])
28+
continue;
29+
elseif(endTime >=interval[0]){
30+
startTime =endTime;
31+
endTime =interval[1];
32+
count +=1;
33+
}
34+
else{
35+
startTime =interval[1] -1;
36+
endTime =interval[1];
37+
count +=2;
38+
}
39+
}
40+
returncount;
41+
}
42+
}
43+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._757;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_757Test {
10+
privatestatic_757.Solutionsolution;
11+
intintervals[][];
12+
13+
@BeforeClass
14+
publicstaticvoidsetup(){
15+
solution =new_757.Solution();
16+
}
17+
@Test
18+
publicvoidtest1() {
19+
intervals =newint [][]{{1,3},{1,4}, {2,5},{3,5}};
20+
assertEquals(3,solution.intersectionSizeTwo(intervals));
21+
}
22+
@Test
23+
publicvoidtest2() {
24+
intervals =newint [][]{{16,18},{11,18}, {15,23},{1,16}, {10,16},{6,19}, {18,20}, {7,19}};
25+
assertEquals(4,solution.intersectionSizeTwo(intervals));
26+
}
27+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp