Skip to content

Commit 73f3431

Browse files
authored
Add solution for 757 (fishercoder1534#124)
* added solution for 757 * added test cases for 757 * updated readme * update readme
1 parent d3ce7de commit 73f3431

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+
package com.fishercoder.solutions;
2+
3+
import java.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+
public class _757 {
20+
public static class Solution{
21+
public int intersectionSizeTwo(int[][] intervals) {
22+
Arrays.sort(intervals, (a, b) -> a[1] == b[1] ? b[0] - a[0]: a[1] - b[1]);
23+
int count = 0;
24+
int startTime = Integer.MIN_VALUE, endTime = Integer.MIN_VALUE;
25+
26+
for(int interval[] : intervals){
27+
if(startTime >= interval[0])
28+
continue;
29+
else if(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+
return count;
41+
}
42+
}
43+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._757;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _757Test {
10+
private static _757.Solution solution;
11+
int intervals[][];
12+
13+
@BeforeClass
14+
public static void setup(){
15+
solution = new _757.Solution();
16+
}
17+
@Test
18+
public void test1() {
19+
intervals = new int [][]{{1,3},{1, 4}, {2,5},{3,5}};
20+
assertEquals(3, solution.intersectionSizeTwo(intervals));
21+
}
22+
@Test
23+
public void test2() {
24+
intervals = new int [][]{{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)