Skip to content

added topKFrequentElements #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ LeetCode

| # | Title | Solution | Difficulty |
|---| ----- | -------- | ---------- |
|347|[Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [C++](./algorithms/cpp/topKFrequentElements/topKFrequentElements.cpp)|Medium|
|345|[Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/) | [C++](./algorithms/cpp/reverseVowelsOfAString/reverseVowelsOfAString.cpp)|Easy|
|337|[House Robber III](https://leetcode.com/problems/house-robber-iii/) | [C++](./algorithms/cpp/houseRobber/houseRobberIII.cpp)|Medium|
|334|[Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/) | [C++](./algorithms/cpp/increasingTripletSubsequence/increasingTripletSubsequence.cpp)|Medium|
Expand Down
65 changes: 65 additions & 0 deletions algorithms/cpp/topKFrequentElements/topKFrequentElements.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Source : https://leetcode.com/problems/top-k-frequent-elements/
// Author : Calinescu Valentin
// Date : 2016-05-02

/***************************************************************************************
*
* Given a non-empty array of integers, return the k most frequent elements.
*
* For example,
* Given [1,1,1,2,2,3] and k = 2, return [1,2].
*
* Note:
* You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
* Your algorithm's time complexity must be better than O(n log n), where n is the
* array's size.
*
***************************************************************************************/

class Solution {
public:
struct element//structure consisting of every distinct number in the vector,
//along with its frequency
{
int number, frequency;
bool operator < (const element arg) const
{
return frequency < arg.frequency;
}
};
priority_queue <element> sol;//we use a heap so we have all of the elements sorted
//by their frequency
vector <int> solution;

vector<int> topKFrequent(vector<int>& nums, int k) {
sort(nums.begin(), nums.end());
int i = 1;
for(; i < nums.size(); i++)
{
int freq = 1;
while(i < nums.size() && nums[i] == nums[i - 1])
{
i++;
freq++;
}
element el;
el.number = nums[i - 1];
el.frequency = freq;
sol.push(el);
}
if(i == nums.size())//if we have 1 distinct element as the last
{
element el;
el.number = nums[nums.size() - 1];
el.frequency = 1;
sol.push(el);
}
while(k)//we extract the first k elements from the heap
{
solution.push_back(sol.top().number);
sol.pop();
k--;
}
return solution;
}
};