-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db675d0
commit eda442c
Showing
3 changed files
with
38 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import re | ||
import collections | ||
import itertools | ||
import heapq | ||
import bisect | ||
import sys | ||
# Definition for singly-linked list. | ||
class ListNode: | ||
def __init__(self, val=0, next=None): | ||
self.val = val | ||
self.next = next | ||
class Solution: | ||
def mergeKLists(self, lists: List[ListNode]) -> ListNode: | ||
heap = [] | ||
root = result = ListNode(None) | ||
|
||
for i, list in enumerate(lists): | ||
if list: | ||
heapq.heappush(heap, (list.val, i, list)) | ||
|
||
while heap: | ||
node = heapq.heappop(heap) | ||
|
||
idx = node[1] | ||
result.next = node[2] | ||
|
||
result = result.next | ||
|
||
if result.next: | ||
heapq.heappush(heap, (result.next.val, idx, result.next)) | ||
|
||
|
||
return root.next |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Solution: | ||
def topKFrequent(self, nums: List[int], k: int) -> List[int]: | ||
return list(zip(*collections.Counter(nums).most_common(k)))[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters