Skip to content

Commit 38c0704

Browse files
committed
Add 88
1 parent b4499d4 commit 38c0704

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

2024/meta/prep.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,3 +1560,33 @@ def numFriendRequests(self, ages: List[int]) -> int:
15601560
left = bisect.bisect_right(ages, 0.5 * a + 7)
15611561
cnt += max(0, right - left - 1)
15621562
return cnt
1563+
1564+
############# 636. Exclusive Time of Functions #############
1565+
class Solution:
1566+
def exclusiveTime(self, n: int, logs: List[str]) -> List[int]:
1567+
"""
1568+
let's look between adjacent events, with duration time - prev_time.
1569+
If we started a function, and we have a function in the background,
1570+
then it was running during this time.
1571+
1572+
Otherwise, we ended the function that is most recent in our stack
1573+
"""
1574+
res = [0] * n
1575+
prevtime = 0
1576+
stack = []
1577+
for log in logs:
1578+
funcid, startend, time = log.split(':')
1579+
funcid = int(funcid)
1580+
time = int(time)
1581+
1582+
if startend == "start":
1583+
if stack:
1584+
res[stack[-1]] += time-prevtime
1585+
stack.append(funcid)
1586+
prevtime = time
1587+
else:
1588+
res[stack.pop()] += time-prevtime + 1
1589+
prevtime = time+1
1590+
1591+
return res
1592+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def intervalIntersection(self, firstList: List[List[int]], secondList: List[List[int]]) -> List[List[int]]:
3+
res = []
4+
i, j = 0, 0
5+
while i < len(firstList) and j < len(secondList):
6+
newstart = max(firstList[i][0], secondList[j][0])
7+
newend = min(firstList[i][1], secondList[j][1])
8+
9+
if newstart <= newend:
10+
res.append((newstart, newend))
11+
if firstList[i][1] < secondList[j][1]:
12+
i += 1
13+
else:
14+
j += 1
15+
return res
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
3+
"""
4+
Do not return anything, modify nums1 in-place instead.
5+
"""
6+
i, j = m-1, n-1
7+
k = len(nums1)-1
8+
9+
while i >= 0 and j >= 0:
10+
if nums1[i] > nums2[j]:
11+
nums1[k] = nums1[i]
12+
i -= 1
13+
else:
14+
nums1[k] = nums2[j]
15+
j -= 1
16+
17+
k -= 1
18+
19+
while j >= 0:
20+
nums1[k] = nums2[j]
21+
k -= 1
22+
j -= 1
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution:
2+
def exclusiveTime(self, n: int, logs: List[str]) -> List[int]:
3+
"""
4+
let's look between adjacent events, with duration time - prev_time.
5+
If we started a function, and we have a function in the background,
6+
then it was running during this time.
7+
8+
Otherwise, we ended the function that is most recent in our stack
9+
"""
10+
res = [0] * n
11+
prevtime = 0
12+
stack = []
13+
for log in logs:
14+
funcid, startend, time = log.split(':')
15+
funcid = int(funcid)
16+
time = int(time)
17+
18+
if startend == "start":
19+
if stack:
20+
res[stack[-1]] += time - prevtime
21+
stack.append(funcid)
22+
prevtime = time
23+
else:
24+
res[stack.pop()] += time + 1 -prevtime
25+
prevtime = time+1
26+
27+
return res
28+

0 commit comments

Comments
 (0)