Skip to content

Commit

Permalink
Merge branch '7oSkaaa:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedGamal2212 committed May 24, 2023
2 parents 98672cb + 215551b commit 1390396
Show file tree
Hide file tree
Showing 14 changed files with 383 additions and 203 deletions.
1 change: 0 additions & 1 deletion 05- May/19- Is Graph Bipartite?/.gitkeep

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Author: Ahmed Hossam

class Solution {
public:

vector<int> topKFrequent(vector<int>& nums, int k) {

// Create a frequency map to count the occurrences of each number
unordered_map < int, int > freq;
for (auto& i : nums)
freq[i]++;

// Sort the numbers based on their frequency in descending order
// If two numbers have the same frequency, sort them in ascending order
sort(nums.begin(), nums.end(), [&](int a, int b) {
return freq[a] > freq[b] || (freq[a] == freq[b] && a < b);
});

// Create a vector to store the top k frequent elements
vector < int > ans = { nums.front() };

// Add unique elements to the vector until it reaches size k
for (int i = 1; i < nums.size(); i++) {
// If the vector has already reached size k, break the loop
if (ans.size() == k) break;

// Add the current element to the vector if it is different from the previous one
if (nums[i] != nums[i - 1])
ans.push_back(nums[i]);
}

// Return the vector containing the top k frequent elements
return ans;
}

};
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Author: Mohamed Emara

class Solution {
public:
// Firstly, We want to calculate the frequency of each value in the vector
// Secondly, We want to sort them in an ascending order
// return a vector of first k elements

vector<int> topKFrequent(vector<int>& nums, int k) {
// Store the frequency of each value in mp
map<int, int>mp;
for(auto it : nums) {
mp[it] ++;
}

// Add the values of mp to tmp but with (value, key)
// to be able to sort them based on the freqecny of each value
vector<pair<int, int>>tmp;
for(auto it : mp)
tmp.push_back({it.second, it.first});

// sort & reverse to guarantee that the first k element have the
// maximum frequecny.
sort(tmp.begin(), tmp.end());
reverse(tmp.begin(), tmp.end());

int cnt = 0;

// Store the values of the most frequent element
vector<int>res;
for(auto it : tmp) {
res.push_back(it.second);
cnt ++;

// if the values taken from tmp = k ---> break.
if(cnt == k)
break;
}
return res;
}
};

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Author: Noura Algohary

class Solution {
public:
// comparison condition to sort in descending order
static bool cmp(pair<int, int>& a, pair<int, int>& b)
{
return a.second > b.second;
}
vector<pair<int, int> > sortMap(map<int, int> map)
{
// Declare vector of pairs
vector<pair<int, int> > orderedMap;

// Copy key-value pair from Map
// to vector of pairs
for (auto& it : M) {
orderedMap.push_back(it);
}

// Sort using comparator function
sort(orderedMap.begin(), orderedMap.end(), cmp);

return orderedMap;
}
vector<int> topKFrequent(vector<int>& nums, int k) {
map<int, int>map; // original frequency map
vector<pair<int, int> > orderedMap;
vector<int>answer;

for(int num : nums)
{
if(map.count(num))
map[num]++;
else
map.insert({num, 1});
}

// store ordered map in a vector of pairs
orderedMap = sortMap(map);

for(auto it=orderedMap.begin(); k > 0 ;it++)
{
answer.push_back(it->first);
k--;
}

return answer;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// author : Omar Sanad

class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
// declare the maximum value that can be found in the array
const int MAX_NUM = 10000;

// declare a frequency array to calc the occurence of all elements in the array
vector < int > fq(MAX_NUM * 2 + 5);

// iterate over the given array nums, update the frequency of the elements in the array
for (auto &I : nums)
fq[I + MAX_NUM]++; // we add the value "MAX_NUM", to avoid accessing negative numbers

// declare an array to store all unique elements in the given array "nums"
vector < int > unq_nums;

// iterate over the frequency array, and add the numbers that appeared in the given array "nums" to "unq_nums"
for (int i = -MAX_NUM; i <= MAX_NUM; i++)
if (fq[i + MAX_NUM])
unq_nums.emplace_back(i);

// sort the "unq_nums" according to the freq of each element using the lamda expression
sort(unq_nums.begin(), unq_nums.end(), [&](const int& a, const int& b){
return fq[a + MAX_NUM] > fq[b + MAX_NUM];
});

// declare an array to store the anwer to the problem
vector < int > Ans;

// add the answer to the problem in the array, the k most frequent elements
for (int i = 0; i < k; i++)
Ans.emplace_back(unq_nums[i]);

// return the answer to the problem
return Ans;
}
};
Loading

0 comments on commit 1390396

Please sign in to comment.