diff --git a/insert-interval/samthekorean.py b/insert-interval/samthekorean.py new file mode 100644 index 000000000..cf4226661 --- /dev/null +++ b/insert-interval/samthekorean.py @@ -0,0 +1,26 @@ +# TC : O(n + m) +# SC : O(n + m) +# n is a size of intervals and m is a size of newInterval +class Solution: + def insert( + self, intervals: List[List[int]], newInterval: List[int] + ) -> List[List[int]]: + result = [] + i = 0 + + while i < len(intervals) and intervals[i][1] < newInterval[0]: + result.append(intervals[i]) + i += 1 + + while i < len(intervals) and intervals[i][0] <= newInterval[1]: + newInterval[0] = min(newInterval[0], intervals[i][0]) + newInterval[1] = max(newInterval[1], intervals[i][1]) + i += 1 + + result.append(newInterval) + + while i < len(intervals): + result.append(intervals[i]) + i += 1 + + return result diff --git a/meeting-rooms-ii/samthekorean.py b/meeting-rooms-ii/samthekorean.py new file mode 100644 index 000000000..20a8868be --- /dev/null +++ b/meeting-rooms-ii/samthekorean.py @@ -0,0 +1,14 @@ +# TS : O(n log n) +# SC : O(n) +class Solution: + def minMeetingRooms(self, intervals: List[List[int]]) -> int: + pairs = [] + for start, end in intervals: + pairs += [(start, 1), (end, -1)] + pairs.sort() + + max_cnt, cnt = 0, 0 + for pair in pairs: + cnt += pair[1] + max_cnt = max(cnt, max_cnt) + return max_cnt diff --git a/merge-intervals/samthekorean.py b/merge-intervals/samthekorean.py new file mode 100644 index 000000000..6fe0e53fe --- /dev/null +++ b/merge-intervals/samthekorean.py @@ -0,0 +1,19 @@ +# TC : O(nlog n) +# SC : O(n) +class Solution: + def merge(self, intervals: List[List[int]]) -> List[List[int]]: + merged = [] + intervals.sort(key=lambda x: x[0]) + + prev = intervals[0] + + for interval in intervals[1:]: + if interval[0] <= prev[1]: + prev[1] = max(prev[1], interval[1]) + else: + merged.append(prev) + prev = interval + + merged.append(prev) + + return merged diff --git a/non-overlapping-intervals/samthekorean.py b/non-overlapping-intervals/samthekorean.py new file mode 100644 index 000000000..3469ce6e0 --- /dev/null +++ b/non-overlapping-intervals/samthekorean.py @@ -0,0 +1,16 @@ +# TC : O(n log n) +# SC : O(n) +class Solution: + def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: + res = 0 + + intervals.sort(key=lambda x: x[1]) + prev_end = intervals[0][1] + + for i in range(1, len(intervals)): + if prev_end > intervals[i][0]: + res += 1 + else: + prev_end = intervals[i][1] + + return res diff --git a/rotate-image/samthekorean.py b/rotate-image/samthekorean.py new file mode 100644 index 000000000..04e8833ec --- /dev/null +++ b/rotate-image/samthekorean.py @@ -0,0 +1,16 @@ +# TC : O(n^2) +# SC : O(1) +class Solution: + def rotate(self, matrix: List[List[int]]) -> None: + top, bottom = 0, len(matrix) - 1 + + while top < bottom: + left, right = top, bottom + for i in range(bottom - top): + 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 = top + 1, bottom - 1