Skip to content

Evan W13 #203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 29, 2024
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
28 changes: 28 additions & 0 deletions insert-interval/evan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from typing import List


class Solution:
def insert(
self, intervals: List[List[int]], newInterval: List[int]
) -> List[List[int]]:
result = []
new_start, new_end = newInterval

for interval in intervals:
current_start, current_end = interval

if current_end < new_start:
result.append(interval)

else:
if current_start > new_end:
result.append([new_start, new_end])
new_start, new_end = interval

else:
new_start = min(new_start, current_start)
new_end = max(new_end, current_end)

result.append([new_start, new_end])

return result
39 changes: 39 additions & 0 deletions meeting-rooms-ii/evan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from typing import List


# Definition of Interval:
class Interval(object):
def __init__(self, start, end):
self.start = start
self.end = end


class Solution:
"""
@param intervals: an array of meeting time intervals
@return: the minimum number of conference rooms required
"""

def min_meeting_rooms(self, intervals: List[Interval]) -> int:
if not intervals:
return 0

start_times = sorted([i.start for i in intervals])
end_times = sorted([i.end for i in intervals])

start_pointer, end_pointer = 0, 0
used_rooms = 0

while start_pointer < len(intervals):
# If there is a meeting that has ended by the time the meeting at `start_pointer` starts
if start_times[start_pointer] >= end_times[end_pointer]:
used_rooms -= 1
end_pointer += 1

# We do this irrespective of whether a room frees up or not.
# If a room got free, then this used_rooms += 1 wouldn't have any effect.
# used_rooms would remain the same in that case.
used_rooms += 1
start_pointer += 1

return used_rooms
23 changes: 23 additions & 0 deletions merge-intervals/evan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from typing import List


class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
if not intervals:
return []

# Sort the intervals by the start time
intervals.sort(key=lambda x: x[0])

merged = []
for interval in intervals:
# If the list of merged intervals is empty
# or if the current interval does not overlap with the previous,
# simply append it.
if not merged or merged[-1][1] < interval[0]:
merged.append(interval)
else:
# Otherwise, there is overlap, so we merge the current and previous intervals.
merged[-1][1] = max(merged[-1][1], interval[1])

return merged
26 changes: 26 additions & 0 deletions non-overlapping-intervals/evan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from typing import List


class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
if not intervals:
return 0

# Sort the intervals based on their end times
# to include as many meetings as possible
intervals.sort(key=lambda x: x[1])

# the count of non-overlapping intervals
count = 0
# the end time of the last added interval
end = float("-inf")

for interval in intervals:
if interval[0] >= end:
# If the current interval does not overlap with the last added interval, include it
end = interval[1]
else:
# Otherwise, increment the count of intervals to be removed
count += 1

return count
18 changes: 18 additions & 0 deletions rotate-image/evan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from typing import List


class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
n = len(matrix)

# Step 1: Transpose the matrix
for i in range(n):
for j in range(i, n):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]

# Step 2: Reverse each row
for i in range(n):
matrix[i].reverse()