From bf35809bb2188f0e179819f999b0eeabdf19f0ab Mon Sep 17 00:00:00 2001 From: bt3gl <138340846+bt3gl-cryptographer@users.noreply.github.com> Date: Tue, 8 Aug 2023 16:44:03 -0700 Subject: [PATCH] Update and rename top_k_frequent_heap.py to top_k_frequent.py --- heaps/top_k_frequent.py | 13 +++++++++++++ heaps/top_k_frequent_heap.py | 17 ----------------- 2 files changed, 13 insertions(+), 17 deletions(-) create mode 100644 heaps/top_k_frequent.py delete mode 100644 heaps/top_k_frequent_heap.py diff --git a/heaps/top_k_frequent.py b/heaps/top_k_frequent.py new file mode 100644 index 0000000..3918f50 --- /dev/null +++ b/heaps/top_k_frequent.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# author: bt3gl + + +def top_k_frequent_values(list, k): + + if k == len(nums): + return nums + + # hashmap element: frequency + counter = Counter(nums) + return heapq.nlargest(k, counter.keys(), key=counter.get) diff --git a/heaps/top_k_frequent_heap.py b/heaps/top_k_frequent_heap.py deleted file mode 100644 index 8acc9d1..0000000 --- a/heaps/top_k_frequent_heap.py +++ /dev/null @@ -1,17 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -# author: bt3gl - -def top_k_frequentnums: list[int], k: int) -> list[int]: - - # O(1) time - if k == len(nums): - return nums - - # 1. build a hashmap element: frequency - counter = Counter(nums) - - # 2. build a heap of k most frequent elements - # 3. build an output array - # O(N log k) time - return heapq.nlargest(k, counter.keys(), key=counter.get)