-
Notifications
You must be signed in to change notification settings - Fork 20
Solution #31 - Daniel/Edited - 11.03.2025 #25
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
Open
danzang100
wants to merge
3
commits into
Dijkstra-Edu:master
Choose a base branch
from
danzang100:cpp
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
Arrays101/#169 - Majority Element - Easy/Majority Element - Solution.cpp
This file contains hidden or 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 @@ | ||
class Solution { | ||
public: | ||
int majorityElement(vector<int>& nums) { | ||
|
||
//Sorting method | ||
|
||
// Hashmap method | ||
unordered_map<int, int> umap; | ||
for(int x: nums){ | ||
umap[x]++; | ||
} | ||
for(auto i: umap){ | ||
if(i.second >= ((double)nums.size()/2)){ | ||
return i.first; | ||
} | ||
} | ||
return 0; | ||
|
||
// Moore's voting method | ||
int count = 0, candidate = nums[0]; | ||
for(int i=1; i<nums.size(); i++){ | ||
if(candidate == nums[i]){ | ||
count++; | ||
} | ||
else{ | ||
if(count == 0){ | ||
candidate = nums[i]; | ||
} | ||
else{ | ||
count--; | ||
} | ||
} | ||
} | ||
return candidate; | ||
} | ||
}; |
26 changes: 26 additions & 0 deletions
26
Arrays101/#169 - Majority Element - Easy/Majority Element.md
This file contains hidden or 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,26 @@ | ||
Simple question of finding an element whose frequency is greater than half of the size of the array. | ||
|
||
Three possible methods: | ||
|
||
1. Sorting: | ||
|
||
- Time Complexity: O(nlogn) | ||
- Space Complexity: O(1) | ||
|
||
Simply sort the array and the middle element should always be the majority element. | ||
|
||
2. Hashmap: | ||
|
||
- Time Complexity: O(n) | ||
- Space Complexity: O(n) | ||
|
||
Map the frequencies of the elements to a hashmap and return the element with the highest frequency. | ||
|
||
3. Moore's Voting Algorithm: | ||
|
||
- Time Complexity: O(n) | ||
- Space Complexity: O(1) | ||
|
||
Initialise count and candidate element. If the element is the candidate element, increment count. Else if count is 0, change the candidate element to be the current element, else decrement the count. | ||
|
||
Works on the principle that upon complete iteration of the array, the majority element should always have the highest count and therefore end up being the candidate element all the time. | ||
20 changes: 20 additions & 0 deletions
20
Arrays101/#31 - Next Permutation - Medium/Next Permutation - Solution.cpp
This file contains hidden or 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,20 @@ | ||
class Solution { | ||
public: | ||
void nextPermutation(vector<int>& nums) { | ||
int i = nums.size()-1; | ||
while(i > 0 && nums[i-1] >= nums[i]){ | ||
i--; | ||
} | ||
|
||
if(i==0){ | ||
reverse(nums.begin(), nums.end()); | ||
return; | ||
} | ||
int j = nums.size()-1; | ||
while(j >= i && nums[j] <= nums[i-1]){ | ||
j--; | ||
} | ||
swap(nums[i-1], nums[j]); | ||
reverse(nums.begin()+i, nums.end()); | ||
} | ||
}; |
7 changes: 7 additions & 0 deletions
7
Arrays101/#31 - Next Permutation - Medium/Next Permutation.md
This file contains hidden or 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,7 @@ | ||
To solve this question, | ||
|
||
1. We find the first decreasing element from the right calling it the pivot. | ||
2. We check if that element does not exist, which means that the array is sorted in non-ascending order, hence we just return a reversed array. | ||
3. If the element exists, we find the smallest element greater than the pivot element which is to the right of it. | ||
4. We swap the two elements. | ||
5. We reverse the suffix of the pivot element to obtain the answer. | ||
Comment on lines
+1
to
+7
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try to maintain consistency with the approach to explanations. Can be much better! |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kindly add the corresponding solutions as well, and also explain the time and space complexity + Try to give a brief on the various algorithms (+ Link articles) on your approach to this.