Skip to content

Commit

Permalink
Priority Queue & Hash Table
Browse files Browse the repository at this point in the history
  • Loading branch information
uniqueimaginate committed Mar 9, 2021
1 parent db675d0 commit eda442c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
33 changes: 33 additions & 0 deletions Coding Test/23.py
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
3 changes: 3 additions & 0 deletions Coding Test/347.py
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]
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
> 책: 20, 739, 225, 622, 316
> 856
4. Deque, Priority Queue
> 책: 23
5. Hash Table
> 771
> 771, 347
6. Graph
> 200, 17, 46, 77, 39, 78, 332, 207
7. Shortest Path
Expand Down

0 comments on commit eda442c

Please sign in to comment.