- Notifications
You must be signed in to change notification settings - Fork1.3k
Add solution for 757#124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionsREADME.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletionssrc/main/java/com/fishercoder/solutions/_757.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.fishercoder.solutions; | ||
import java.util.Arrays; | ||
/** | ||
* Problem 757: Set Intersection Size At Least Two | ||
* An integer interval [a, b] (for integers a < b) is a set of all consecutive integers from a to b, including a and b. | ||
* 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. | ||
*/ | ||
/** | ||
* Approach: Sort the intervals in the ascending order of end range. | ||
* In case if the end range of any 2 intervals match, | ||
* sort those intervals based on the descending order of start range | ||
* e.g. intervals = [[1, 3], [1, 4], [2, 5], [3, 5]] | ||
* After sorting, intervals[] becomes = [[1,3], [1,4], [3,5],[2,5]] | ||
* 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 | ||
*/ | ||
public class _757 { | ||
public static class Solution{ | ||
public int intersectionSizeTwo(int[][] intervals) { | ||
Arrays.sort(intervals, (a, b) -> a[1] == b[1] ? b[0] - a[0]: a[1] - b[1]); | ||
int count = 0; | ||
int startTime = Integer.MIN_VALUE, endTime = Integer.MIN_VALUE; | ||
for(int interval[] : intervals){ | ||
if(startTime >= interval[0]) | ||
continue; | ||
else if(endTime >= interval[0]){ | ||
startTime = endTime; | ||
endTime = interval[1]; | ||
count += 1; | ||
} | ||
else{ | ||
startTime = interval[1] - 1; | ||
endTime = interval[1]; | ||
count += 2; | ||
} | ||
} | ||
return count; | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletionssrc/test/java/com/fishercoder/_757Test.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.fishercoder; | ||
import com.fishercoder.solutions._757; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import static org.junit.Assert.assertEquals; | ||
public class _757Test { | ||
private static _757.Solution solution; | ||
int intervals[][]; | ||
@BeforeClass | ||
public static void setup(){ | ||
solution = new _757.Solution(); | ||
} | ||
@Test | ||
public void test1() { | ||
intervals = new int [][]{{1,3},{1, 4}, {2,5},{3,5}}; | ||
assertEquals(3, solution.intersectionSizeTwo(intervals)); | ||
} | ||
@Test | ||
public void test2() { | ||
intervals = new int [][]{{16,18},{11, 18}, {15,23},{1,16}, {10,16},{6,19}, {18,20}, {7,19}}; | ||
assertEquals(4, solution.intersectionSizeTwo(intervals)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.