This file contains hidden or 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
| class TrieNode: | |
| def __init__(self): | |
| self.children = {} | |
| self.ref = 0 # number of references (ie. size of each map) | |
| self.end = False | |
| def add_word(self, word): | |
| node = self | |
| node.ref += 1 | |
| for c in word: |
This file contains hidden or 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
| class MaxHeap(list): | |
| def __init__(self): | |
| self = [] | |
| def push(self, n): | |
| heapq.heappush(self, -n) | |
| def pop(self): | |
| return -heapq.heappop(self) | |
| def peek(self): | |
| return -self[0] |
This file contains hidden or 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
| class TrieNode: | |
| def __init__(self): | |
| self.children = {} | |
| self.ref = 0 # number of references (ie. size of each map) | |
| self.end = False | |
| def add_word(self, word): | |
| node = self | |
| node.ref += 1 | |
| for c in word: |
This file contains hidden or 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
| class Solution: | |
| def topKFrequent(self, nums: List[int], k: int) -> List[int]: | |
| # sorting | |
| ''' | |
| nums_map = collections.Counter(nums) | |
| return sorted(nums_map, key=nums_map.get)[-k:] | |
| # Equivalent to: heapq.nlargest(k, nums_map.keys(), key=count.get) | |
| ''' | |
| # heap way (equivalent to above) | |
| ''' |