Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions insert-interval/bky373.java
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()][]);
}
}
35 changes: 35 additions & 0 deletions meeting-rooms-ii/bky373.java
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();
}
}
19 changes: 19 additions & 0 deletions merge-intervals/bky373.java
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()][]);
}
}
22 changes: 22 additions & 0 deletions non-overlapping-intervals/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// time: O(n * log n)
// space: O(log n)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํ˜น์‹œ space๊ฐ€ log n ์œผ๋กœ ๋‚˜์˜จ ์ด์œ ๊ฐ€ ๋ญ˜๊นŒ์š”?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์•„ ์ €๋„ ๊นŠ์ด ํŒŒ๋ณธ ๊ฑด ์•„๋‹ˆ์ง€๋งŒ Arrays.sort() ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ž„์‹œ ์ €์žฅ ๊ณต๊ฐ„์œผ๋กœ 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;
}
}
17 changes: 17 additions & 0 deletions rotate-image/bky373.java
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;
}
}
}
}