Skip to content

Commit e5caf34

Browse files
Time: 179 ms (93.57%) | Memory: 23.3 MB (37.22%) - LeetSync
1 parent 271550d commit e5caf34

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def smallestRange(self, nums: List[List[int]]) -> List[int]:
3+
k = len(nums)
4+
qu = []
5+
maxi = -float("inf")
6+
resStart, resEnd = 0, float("inf")
7+
8+
for i in range(k):
9+
heappush(qu, (nums[i][0], i, 0))
10+
maxi = max(maxi, nums[i][0])
11+
12+
lenQu = k
13+
while lenQu == k:
14+
mini, ind, col = heappop(qu)
15+
lenQu -= 1
16+
if abs(mini - maxi) < abs(resStart - resEnd):
17+
resStart = mini
18+
resEnd = maxi
19+
20+
if col+1 < len(nums[ind]):
21+
heappush(qu, (nums[ind][col+1], ind, col+1))
22+
maxi = max(maxi, nums[ind][col+1])
23+
lenQu += 1
24+
25+
return [resStart, resEnd]

0 commit comments

Comments
 (0)