summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com>2024-08-26 15:57:39 +0900
committerTheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com>2024-08-26 15:57:39 +0900
commit8225172fc086650ca5d49ec96d03d3bc22077ee1 (patch)
tree8558f31b8c2a611fec82897571d75cb32eba23d3
parenta1b80e66b43ed77c34357af31686697322421995 (diff)
Init
-rw-r--r--1_array_hashing/group_anagram.py4
-rw-r--r--1_array_hashing/top_k_elements.py69
-rw-r--r--1_array_hashing/two_sum.py16
-rw-r--r--1_array_hashing/valid_anagram.py4
4 files changed, 67 insertions, 26 deletions
diff --git a/1_array_hashing/group_anagram.py b/1_array_hashing/group_anagram.py
index 2276799..f2085b2 100644
--- a/1_array_hashing/group_anagram.py
+++ b/1_array_hashing/group_anagram.py
@@ -64,8 +64,8 @@ url: https://neetcode.io/problems/anagram-groups
video: https://youtu.be/vzdNOK2oB2E
1. dictionary
-time:
-space:
+time: O(m*n*26) = O(m*n)
+space: O(m*n)
code:
```python
class Solution:
diff --git a/1_array_hashing/top_k_elements.py b/1_array_hashing/top_k_elements.py
index e1bd5b8..ba17626 100644
--- a/1_array_hashing/top_k_elements.py
+++ b/1_array_hashing/top_k_elements.py
@@ -3,6 +3,8 @@ Question
Top K Elements in List
+Medium
+
Given an integer array nums and an integer k, return the k most frequent elements within the array.
The test cases are generated such that the answer is always unique.
@@ -30,32 +32,71 @@ Constraints:
class Solution:
- def fn_name(self, parameters) -> return_type:
- return return
+ """
+ A class to solve top_k_elements from hash
+ """
+
+ def hashmap(self, nums: List[int], k: int) -> List[int]:
+ """
+ Frequent elements from the input nums: List[int] and k: int using a hashmap approach.
+ Args:
+ nums: List[int]: integer list
+ k: int: frequency
+ Returns:
+ List[int]: elements list
+ """
+ return [-1, -1]
-case1 = case1
-case2 = case2
-case3 = case3
+
+case1 = [1, 2, 2, 3, 3, 3]
+k1 = 2
+case2 = [7, 7]
+k2 = 1
+case3 = [0, 9, 0, 6, 0, 4]
+k3 = 3
solution = Solution()
-print(f" fn_name case1: {solution.fn_name(case1args)}")
-print(f" fn_name case2: {solution.fn_name(case2args)}")
-print(f" fn_name case3: {solution.fn_name(case3args)}")
+print(f" hashmap case1: {solution.hashmap(case1,k1)}")
+print(f" hashmap case2: {solution.hashmap(case2,k2)}")
+print(f" hashmap case3: {solution.hashmap(case3,k3)}")
"""
Solution
-url: url
-video: video
+url: https://neetcode.io/problems/top-k-elements-in-list
+video: https://youtu.be/YPTqKIgVk-k
-1.
-time:
-space:
+1. Bucket sort
+time: O(n)
+space: O(n)
code:
```python
-
+class Solution:
+ def topKFrequent(self, nums: List[int], k: int) -> List[int]:
+ count = {}
+ freq = [[] for i in range(len(nums) + 1)]
+
+ for n in nums:
+ count[n] = 1 + count.get(n, 0)
+ for n, c in count.items():
+ freq[c].append(n)
+
+ res = []
+ for i in range(len(freq) - 1, 0, -1):
+ for n in freq[i]:
+ res.append(n)
+ if len(res) == k:
+ return res
```
+
+2. Heap sort (max heap)
+time: O(k*logn)
+space: O(k*logn)
+
+3. Sorting
+time: O(n*logn)
+space: O(n*logn)
"""
diff --git a/1_array_hashing/two_sum.py b/1_array_hashing/two_sum.py
index 534ec87..81b7278 100644
--- a/1_array_hashing/two_sum.py
+++ b/1_array_hashing/two_sum.py
@@ -42,12 +42,12 @@ from typing import List
class Solution:
def hashmap(self, nums: List[int], target: int) -> List[int]:
- mp = {}
- for i, n in enumerate(nums):
- diff = target - n
- if diff in mp:
- return [mp[diff], i]
- mp[nums[i]] = i
+ hm: dict = {}
+ for i, index in enumerate(nums):
+ diff = target - index
+ if diff in hm:
+ return [hm[diff], i]
+ hm[index] = i
return [-1, -1]
@@ -71,8 +71,8 @@ url: https://neetcode.io/problems/two-integer-sum
video: https://youtu.be/KLlXCFG5TnA
1. hashmap
-time:
-space:
+time: O(n)
+space: O(n)
code:
```python
class Solution:
diff --git a/1_array_hashing/valid_anagram.py b/1_array_hashing/valid_anagram.py
index 21a756e..3305088 100644
--- a/1_array_hashing/valid_anagram.py
+++ b/1_array_hashing/valid_anagram.py
@@ -34,8 +34,8 @@ class Solution:
countS: dict = {}
countT: dict = {}
- for i in range(len(s)):
- countS[s[i]] = 1 + countS.get(s[i], 0)
+ for i, si in enumerate(s):
+ countS[si] = 1 + countS.get(si, 0)
countT[t[i]] = 1 + countT.get(t[i], 0)
return countS == countT