-
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.
[Sehwan] Week13 solution with JavaScript
- Loading branch information
Showing
5 changed files
with
121 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 @@ | ||
var insert = function (intervals, newInterval) { | ||
let result = []; | ||
let i = 0; | ||
|
||
// 1. Add all intervals before the new interval | ||
while (i < intervals.length && intervals[i][1] < newInterval[0]) { | ||
result.push(intervals[i]); | ||
i++; | ||
} | ||
|
||
// 2. Merge all overlapping intervals with new interval | ||
while (i < intervals.length && intervals[i][0] <= newInterval[1]) { | ||
newInterval[0] = Math.min(newInterval[0], intervals[i][0]); | ||
newInterval[1] = Math.max(newInterval[1], intervals[i][1]); | ||
i++; | ||
} | ||
result.push(newInterval); | ||
|
||
// 3. Add all intervals after the new interval | ||
while (i < intervals.length && intervals[i][1] >= newInterval[0]) { | ||
result.push(intervals[i]); | ||
i++; | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
// TC = O(n) | ||
// SC = O(n) |
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 @@ | ||
class Solution { | ||
/** | ||
* @param {Interval[]} intervals | ||
* @returns {number} | ||
*/ | ||
minMeetingRooms(intervals) { | ||
// Edge case | ||
if (intervals.length === 0) return 0; | ||
|
||
let startTimes = intervals.map((interval) => interval.start); | ||
let endTimes = intervals.map((interval) => interval.end); | ||
|
||
startTimes.sort((a, b) => a - b); | ||
endTimes.sort((a, b) => a - b); | ||
|
||
let roomNeeded = 0; | ||
let endIndex = 0; | ||
|
||
for (let i = 0; i < intervals.length; i++) { | ||
if (startTimes[i] < endTimes[endIndex]) roomNeeded++; | ||
else endIndex++; | ||
} | ||
|
||
return roomNeeded; | ||
} | ||
} | ||
|
||
// TC: O(nlogn) | ||
// SC: O(n) |
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 @@ | ||
var merge = function (intervals) { | ||
// Sort intervals with first start number | ||
intervals.sort((a, b) => a[0] - b[0]); | ||
let result = []; | ||
let currentInterval = intervals[0]; | ||
|
||
for (let i = 1; i < intervals.length; i++) { | ||
if (currentInterval[1] >= intervals[i][0]) { | ||
currentInterval[1] = Math.max(currentInterval[1], intervals[i][1]); | ||
} else { | ||
result.push(currentInterval); | ||
currentInterval = intervals[i]; | ||
} | ||
} | ||
|
||
result.push(currentInterval); | ||
|
||
return result; | ||
}; | ||
|
||
// TC: O(nlogn) | ||
// SC: O(n) |
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,18 @@ | ||
var eraseOverlapIntervals = function (intervals) { | ||
let count = 0; | ||
intervals.sort((a, b) => a[1] - b[1]); | ||
let currentEnd = intervals[0][1]; | ||
|
||
for (let i = 1; i < intervals.length; i++) { | ||
if (currentEnd > intervals[i][0]) { | ||
count++; | ||
} else { | ||
currentEnd = intervals[i][1]; | ||
} | ||
} | ||
|
||
return count; | ||
}; | ||
|
||
// TC: O(nlogn) | ||
// SC: O(1) |
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,23 @@ | ||
var rotate = function (matrix) { | ||
let top = 0, | ||
bottom = matrix.length - 1; | ||
let left, right; | ||
|
||
while (top < bottom) { | ||
(left = top), (right = bottom); | ||
|
||
for (let i = 0; i < bottom - top; i++) { | ||
const topLeft = matrix[top][left + i]; | ||
matrix[top][left + i] = matrix[bottom - i][left]; | ||
matrix[bottom - i][left] = matrix[bottom][right - i]; | ||
matrix[bottom][right - i] = matrix[top + i][right]; | ||
matrix[top + i][right] = topLeft; | ||
} | ||
|
||
top++; | ||
bottom--; | ||
} | ||
}; | ||
|
||
// TC: O(n^2) | ||
// SC: O(1) |