Skip to content

Commit

Permalink
Top K Frequent Elements - Doc
Browse files Browse the repository at this point in the history
  • Loading branch information
dksifoua committed May 19, 2024
1 parent e8bd4e5 commit cca817f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ int sum(int[] A, int n) {
- **Example:** Quick sort and Merge sort.

- **Dynamic Programming Algorithms**
- Almost same as divide and conquer but they use memorization to solve each sub-problems only one time.
- Almost same as divide and conquer, but they use memorization to solve each sub-problems only one time.
- The algorithm saves the past sub-problem solutions to find the next sub-problem solutions.
- These types of algorithms are generally used for optimization problems.
- The goal is to find the best solution among multiple solutions.
Expand All @@ -68,14 +68,15 @@ int sum(int[] A, int n) {

## Solutions

| ID | Difficulty | Problem | Topics | Solution Link |
|------|------------|---------------------------------|-----------------------------------|-----------------------------------------------------------|
| 0001 | Easy | Two Sum | Array, HashMap | [solution](./docs/0001-Two-Sum.md) |
| 0002 | Medium | Add Two numbers | LinkedList, Recursion | |
| 0049 | Medium | Group Anagrams | Array, HashTable, String, Sorting | [solution](/docs/0049-Group-Anagrams.md ) |
| 0121 | Easy | Best Time to Buy and Sell Stock | Array | [solution](/docs/0121-Best-Time-to-Buy-and-Sell-Stock.md) |
| 0125 | Easy | Valid Palindrome | String, Two Pointers | [solution](./docs/0125-Valid-Palindrome.md) |
| 0169 | Easy | Majority Element | Array, HashTable, Counting | [solution](./docs/0169-Majority-Element.md) |
| 0217 | Easy | Contains Duplicate | Array | [solution](./docs/0217-Contains-Duplicate.md) |
| 0219 | Easy | Contains Duplicate II | Array | [solution](./docs/0219-Contains-Duplicate-II.md) |
| 0242 | Easy | Valid Anagram | String, HashTable | [solution](./docs/0242-Valid-Anagram.md) |
| ID | Difficulty | Problem | Topics | Link |
|------|------------|---------------------------------|-----------------------------------|------------------------------------------------------------|
| 0001 | Easy | Two Sum | Array, HashMap | [solution](./docs/0001-Two-Sum.md) |
| 0002 | Medium | Add Two numbers | LinkedList, Recursion | |
| 0049 | Medium | Group Anagrams | Array, HashTable, String, Sorting | [solution](./docs/0049-Group-Anagrams.md ) |
| 0121 | Easy | Best Time to Buy and Sell Stock | Array | [solution](./docs/0121-Best-Time-to-Buy-and-Sell-Stock.md) |
| 0125 | Easy | Valid Palindrome | String, Two Pointers | [solution](./docs/0125-Valid-Palindrome.md) |
| 0169 | Easy | Majority Element | Array, HashTable, Counting | [solution](./docs/0169-Majority-Element.md) |
| 0217 | Easy | Contains Duplicate | Array | [solution](./docs/0217-Contains-Duplicate.md) |
| 0219 | Easy | Contains Duplicate II | Array | [solution](./docs/0219-Contains-Duplicate-II.md) |
| 0242 | Easy | Valid Anagram | String, HashTable | [solution](./docs/0242-Valid-Anagram.md) |
| 0347 | Medium | Top K Frequent Elements | Array, HashMap, Bucket Sort | [Solution](./docs/0347-Top-K-Frequent-Elements.md) |
33 changes: 33 additions & 0 deletions docs/0347-Top-K-Frequent-Elements.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/description/)

## Intuition

The problem requires finding the k most frequent elements in an array. A direct way to do this is by counting the frequency of each element and then sorting or selecting the top k elements based on their frequencies. A more efficient approach leverages bucket sort, where elements are grouped by their frequencies. This way, we can directly access elements with the highest frequencies without sorting all the elements.

## Approach

1. **Counting Frequencies:**
- Use a HashMap to store the frequency of each element in the array.
- Iterate through the array and populate the HashMap with the count of each element.
2. **Bucket Sort:**
- Create an array of lists (bucket) where the index represents the frequency of elements. The length of this array is `nums.length + 1` because the maximum possible frequency of any element can be `nums.length` (if all elements are the same).
- Iterate over the HashMap and place each element into the corresponding bucket based on its frequency. The bucket index is frequency.
3. **Collecting Results:**
- Start from the highest possible frequency (end of the bucket array) and collect elements until we have k elements.
- Traverse the bucket array from the end to the beginning, and for each non-null bucket, add its elements to the result array.
- Stop collecting once we have added k elements to the result array.

## Complexity

- **Time Complexity: `O(N)`**
- Counting the frequencies takes `O(N)` time, where `N` is the number of elements in the input array.
- Filling the buckets takes `O(N)` time.
- Collecting the top `k` frequent elements takes `O(N)` time in the worst case.
- **Space Complexity: `O(N)`**
- The HashMap for counting frequencies takes `O(N)` space.
- The bucket array of lists also takes `O(N)` space in the worst case.
- The result array takes `O(k)` space.

## Code

- [Java](../src/main/java/io/dksifoua/leetcode/topkfrequentelements/Solution.java)

0 comments on commit cca817f

Please sign in to comment.