diff --git a/insert-interval/yolophg.js b/insert-interval/yolophg.js new file mode 100644 index 000000000..e591c0a47 --- /dev/null +++ b/insert-interval/yolophg.js @@ -0,0 +1,30 @@ +// Time Complexity: O(n log n) +// Space Complexity: O(n) + +var insert = function (intervals, newInterval) { + // append the newInterval to the intervals array + intervals.push(newInterval); + + // sort the intervals array by start time + intervals.sort((a, b) => a[0] - b[0]); + + // to store merged intervals + let result = []; + + // iterate through the sorted intervals array + for (let i = 0; i < intervals.length; i++) { + // if the result array is empty or the current interval does not overlap with the last interval + if (result.length === 0 || result[result.length - 1][1] < intervals[i][0]) { + result.push(intervals[i]); // Add the current interval to the result array + } else { + // if there is an overlap, merge the current interval with the last interval + result[result.length - 1][1] = Math.max( + result[result.length - 1][1], + intervals[i][1] + ); + } + } + + // return the final merged intervals + return result; +}; diff --git a/meeting-rooms-ii/yolophg.js b/meeting-rooms-ii/yolophg.js new file mode 100644 index 000000000..ef5433df8 --- /dev/null +++ b/meeting-rooms-ii/yolophg.js @@ -0,0 +1,38 @@ +// Time Complexity: O(n log n) +// Space Complexity: O(n) + +export class Solution { + minMeetingRooms(intervals) { + // separate start and end times into different arrays + let startTimes = intervals.map((interval) => interval[0]); + let endTimes = intervals.map((interval) => interval[1]); + + // sort the start and end times + startTimes.sort((a, b) => a - b); + endTimes.sort((a, b) => a - b); + + let startPointer = 0; + let endPointer = 0; + let rooms = 0; + let maxRooms = 0; + + // iterate over all the start times + while (startPointer < intervals.length) { + // if the start time is less than the end time, a new room is needed + if (startTimes[startPointer] < endTimes[endPointer]) { + rooms++; + startPointer++; + } else { + // if the start time is not less than the end time, free up a room + rooms--; + endPointer++; + } + + // update the maximum number of rooms needed + maxRooms = Math.max(maxRooms, rooms); + } + + // return the maximum number of rooms needed + return maxRooms; + } +} diff --git a/merge-intervals/yolophg.js b/merge-intervals/yolophg.js new file mode 100644 index 000000000..ab9504644 --- /dev/null +++ b/merge-intervals/yolophg.js @@ -0,0 +1,31 @@ +// Time Complexity: O(n log n) +// Space Complexity: O(n) + +var merge = function (intervals) { + // sort the intervals by their start time + intervals.sort((a, b) => a[0] - b[0]); + + // to manage merged intervals + let stack = []; + + // push the first interval onto the stack + stack.push(intervals[0]); + + // iterate through the sorted intervals + for (let i = 1; i < intervals.length; i++) { + // get the top interval from the stack + let top = stack[stack.length - 1]; + + // if the current interval overlaps with the top interval + if (top[1] >= intervals[i][0]) { + // merge the intervals by updating the end of the top interval + top[1] = Math.max(top[1], intervals[i][1]); + } else { + // if there is no overlap, push the current interval onto the stack + stack.push(intervals[i]); + } + } + + // return the final merged intervals + return stack; +}; diff --git a/non-overlapping-intervals/yolophg.js b/non-overlapping-intervals/yolophg.js new file mode 100644 index 000000000..c78dbf0a8 --- /dev/null +++ b/non-overlapping-intervals/yolophg.js @@ -0,0 +1,23 @@ +// Time Complexity: O(n log n) +// Space Complexity: O(1) + +var eraseOverlapIntervals = function (intervals) { + // sort the intervals based on their end times + intervals.sort((a, b) => a[1] - b[1]); + + let end = intervals[0][1]; + let count = 0; + + // iterate through the sorted intervals + for (let i = 1; i < intervals.length; i++) { + if (intervals[i][0] < end) { + // overlapping interval found, increment the count + count++; + } else { + // no overlap, update the end to the current interval's end + end = intervals[i][1]; + } + } + + return count; +}; diff --git a/rotate-image/yolophg.js b/rotate-image/yolophg.js new file mode 100644 index 000000000..91239fedc --- /dev/null +++ b/rotate-image/yolophg.js @@ -0,0 +1,31 @@ +// Time Complexity: O(n^2) +// Space Complexity: O(1) + +var rotate = function (matrix) { + const n = matrix.length; + + // perform the rotation layer by layer + for (let layer = 0; layer < Math.floor(n / 2); layer++) { + let first = layer; + let last = n - 1 - layer; + + for (let i = first; i < last; i++) { + let offset = i - first; + + // save the top element + let top = matrix[first][i]; + + // move left element to top + matrix[first][i] = matrix[last - offset][first]; + + // move bottom element to left + matrix[last - offset][first] = matrix[last][last - offset]; + + // move right element to bottom + matrix[last][last - offset] = matrix[i][last]; + + // assign saved top element to right + matrix[i][last] = top; + } + } +};