forked from 7oSkaaa/LeetCode_DailyChallenge_2023
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '7oSkaaa:main' into main
- Loading branch information
Showing
14 changed files
with
383 additions
and
203 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
46 changes: 0 additions & 46 deletions
46
05- May/19- Is Graph Bipartite?/19- Is Graph Bipartite? (Ahmed Hossam).cpp
This file was deleted.
Oops, something went wrong.
49 changes: 0 additions & 49 deletions
49
05- May/19- Is Graph Bipartite?/19- Is Graph Bipartite? (Lama Salah).cpp
This file was deleted.
Oops, something went wrong.
58 changes: 0 additions & 58 deletions
58
05- May/19- Is Graph Bipartite?/19- Is Graph Bipartite? (Mahmoud aboelsoud).cpp
This file was deleted.
Oops, something went wrong.
49 changes: 0 additions & 49 deletions
49
05- May/19- Is Graph Bipartite?/19- Is Graph Bipartite? (Omar Sanad).cpp
This file was deleted.
Oops, something went wrong.
36 changes: 36 additions & 0 deletions
36
05- May/22- Top K Frequent Elements/22- Top K Frequent Elements (Ahmed Hossam).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
}; |
42 changes: 42 additions & 0 deletions
42
05- May/22- Top K Frequent Elements/22- Top K Frequent Elements (Mohamed Emara).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; | ||
|
50 changes: 50 additions & 0 deletions
50
05- May/22- Top K Frequent Elements/22- Top K Frequent Elements (Noura Algohary).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
39 changes: 39 additions & 0 deletions
39
05- May/22- Top K Frequent Elements/22- Top K Frequent Elements (Omar Sanad).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
Oops, something went wrong.