-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,29 @@ | ||
// time: O(N) | ||
// space: O(1) | ||
class Solution { | ||
|
||
public int[][] insert(int[][] intervals, int[] newInterval) { | ||
int n = intervals.length; | ||
int i = 0; | ||
List<int[]> result = new ArrayList<>(); | ||
|
||
while (i < n && intervals[i][1] < newInterval[0]) { | ||
result.add(intervals[i]); | ||
i++; | ||
} | ||
|
||
while (i < n && newInterval[1] >= intervals[i][0]) { | ||
newInterval[0] = Math.min(newInterval[0], intervals[i][0]); | ||
newInterval[1] = Math.max(newInterval[1], intervals[i][1]); | ||
i++; | ||
} | ||
result.add(newInterval); | ||
|
||
while (i < n) { | ||
result.add(intervals[i]); | ||
i++; | ||
} | ||
|
||
return result.toArray(new int[result.size()][]); | ||
} | ||
} |
This file contains 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,35 @@ | ||
// time: O(n * log n) | ||
// space: O(n) | ||
class Solution { | ||
|
||
public int minMeetingRooms(int[][] intervals) { | ||
if (intervals.length == 0) { | ||
return 0; | ||
} | ||
|
||
PriorityQueue<Integer> pq = new PriorityQueue<Integer>( | ||
intervals.length, | ||
new Comparator<Integer>() { | ||
public int compare(Integer a, Integer b) { | ||
return a - b; | ||
} | ||
}); | ||
|
||
Arrays.sort(intervals, new Comparator<int[]>() { | ||
public int compare(final int[] a, final int[] b) { | ||
return a[0] - b[0]; | ||
} | ||
}); | ||
|
||
pq.add(intervals[0][1]); | ||
|
||
for (int i = 1; i < intervals.length; i++) { | ||
if (intervals[i][0] >= pq.peek()) { | ||
pq.poll(); | ||
} | ||
pq.add(intervals[i][1]); | ||
} | ||
|
||
return pq.size(); | ||
} | ||
} |
This file contains 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,19 @@ | ||
// time: O(n * log n) | ||
// space: O(log n) | ||
class Solution { | ||
|
||
public int[][] merge(int[][] intervals) { | ||
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0])); | ||
List<int[]> merged = new ArrayList<>(); | ||
|
||
for (int[] interval : intervals) { | ||
if (merged.isEmpty() || merged.getLast()[1] < interval[0]) { | ||
merged.add(interval); | ||
} else { | ||
merged.getLast()[1] = Math.max(merged.getLast()[1], interval[1]); | ||
} | ||
} | ||
|
||
return merged.toArray(new int[merged.size()][]); | ||
} | ||
} |
This file contains 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,22 @@ | ||
// time: O(n * log n) | ||
// space: O(log n) | ||
class Solution { | ||
public int eraseOverlapIntervals(int[][] intervals) { | ||
Arrays.sort(intervals, Comparator.comparingInt(a -> a[1])); | ||
int count = 0; | ||
int min = Integer.MIN_VALUE; | ||
|
||
for (int i=0; i<intervals.length; i++) { | ||
int x = intervals[i][0]; | ||
int y = intervals[i][1]; | ||
|
||
if (x >= min) { | ||
min = y; | ||
} else { | ||
count++; | ||
} | ||
} | ||
|
||
return count; | ||
} | ||
} |
This file contains 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,17 @@ | ||
// time: O(N), where N is the number of cells in the matrix | ||
// space: O(1) | ||
class Solution { | ||
|
||
public void rotate(int[][] matrix) { | ||
int n = matrix.length; | ||
for (int i = 0; i < (n + 1) / 2; i++) { | ||
for (int j = 0; j < n / 2; j++) { | ||
int temp = matrix[n - 1 - j][i]; | ||
matrix[n - 1 - j][i] = matrix[n - 1 - i][n - j - 1]; | ||
matrix[n - 1 - i][n - j - 1] = matrix[j][n - 1 - i]; | ||
matrix[j][n - 1 - i] = matrix[i][j]; | ||
matrix[i][j] = temp; | ||
} | ||
} | ||
} | ||
} |