Skip to content

Commit eb32408

Browse files
committed
Added question 253 and 767.
1 parent a8e6557 commit eb32408

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# 253. Meeting Rooms II
2+
3+
## Sorting Solution
4+
- Runtime: O(Nlog(N))
5+
- Space: O(N)
6+
- N = Number of total start and end points in intervals
7+
8+
By separating the start and end points of each interval, we can then sort them based on their time, if the times are the same, we can break them depending on if its a start or end point.
9+
10+
This allows us to perform one pass over the sorted points to find number of occupied rooms.
11+
If its a start point, we can increment the number of rooms and vice versa.
12+
13+
The one edge case to consider is an input like [[0,5], [5,10]] which results in 1 room needed.
14+
Notice that the start and end points overlap between the two intervals. This means when sorting the points, we should give precedence for end points over start points during tiebreakers.
15+
16+
```
17+
from collections import namedtuple
18+
19+
Point = namedtuple('Point', ['time', 'is_start'])
20+
21+
class Solution:
22+
def minMeetingRooms(self, intervals: List[List[int]]) -> int:
23+
points = [Point(time=x[0], is_start=1) for x in intervals] + [Point(time=x[1], is_start=0) for x in intervals]
24+
points.sort(key=lambda x: (x.time, x.is_start))
25+
n_used_rooms = max_rooms = 0
26+
for point in points:
27+
if point.is_start:
28+
n_used_rooms += 1
29+
else:
30+
n_used_rooms -= 1
31+
max_rooms = max(max_rooms, n_used_rooms)
32+
return max_rooms
33+
```
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# 767. Reorganize String
2+
3+
## Greedy Heap Solution
4+
- Runtime: O(Nlog(N))
5+
- Space: O(N)
6+
- N = Number of characters in string
7+
8+
Playing around with different examples like 'aabb', 'aabbc', 'aabbcc' or 'aaabbc'.
9+
We can see a pattern where a greedy approach can be taken.
10+
We can build the string by taking the most occurring element.
11+
This leads us to using a max heap to determine this.
12+
We will need a dictionary to count the occurrences and use that to store tuples of (occurrences, character) pairs into the heap.
13+
14+
Another thing we notice is that there can be a scenario where the most occurring element on top of the heap is the same as the last character we just used to build the string, for example, 'aaaabb'. This means that we need to pop two elements from the heap to guarantee that we can use one of these characters.
15+
16+
Last case is when we cannot build a valid string.
17+
This can be determined if the occurrence of the last element in the heap is of one or not after the above sub-solution has be done processing and created the longest valid string possible.
18+
19+
```
20+
from collections import Counter
21+
22+
class Solution:
23+
def reorganizeString(self, S: str) -> str:
24+
counter = Counter(S)
25+
max_heap = list((-v, k) for k, v in counter.items())
26+
heapq.heapify(max_heap)
27+
str_builder = list()
28+
while len(max_heap) >= 2:
29+
val1, ch1 = heapq.heappop(max_heap)
30+
val2, ch2 = heapq.heappop(max_heap)
31+
if len(str_builder) == 0 or (len(str_builder) and str_builder[-1] != ch1):
32+
str_builder.append(ch1)
33+
if val1 != -1:
34+
heapq.heappush(max_heap, (val1+1, ch1))
35+
else:
36+
heapq.heappush(max_heap, (val1, ch1))
37+
if len(str_builder) and str_builder[-1] != ch2:
38+
str_builder.append(ch2)
39+
if val2 != -1:
40+
heapq.heappush(max_heap, (val2+1, ch2))
41+
else:
42+
heapq.heappush(max_heap, (val2, ch2))
43+
if len(max_heap): # last node in heap
44+
val, ch = heapq.heappop(max_heap)
45+
if val != -1:
46+
return ''
47+
else:
48+
str_builder.append(ch)
49+
return ''.join(str_builder)
50+
```

0 commit comments

Comments
 (0)