Solution #31 - Daniel/Edited - 11.03.2025#25
Open
danzang100 wants to merge 3 commits intoDijkstra-Edu:masterfrom
Open
Solution #31 - Daniel/Edited - 11.03.2025#25danzang100 wants to merge 3 commits intoDijkstra-Edu:masterfrom
danzang100 wants to merge 3 commits intoDijkstra-Edu:masterfrom
Conversation
Author
|
2 problems to be added. |
JRS296
requested changes
Mar 17, 2025
Member
JRS296
left a comment
There was a problem hiding this comment.
Follow the instructions that were shared with the other PR
JRS296
reviewed
Mar 21, 2025
Comment on lines
+2
to
+26
|
|
||
| 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. |
Member
There was a problem hiding this comment.
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.
JRS296
reviewed
Mar 21, 2025
Comment on lines
+1
to
+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. |
Member
There was a problem hiding this comment.
Try to maintain consistency with the approach to explanations. Can be much better!
JRS296
requested changes
Mar 21, 2025
Member
JRS296
left a comment
There was a problem hiding this comment.
Try to maintain consistency with how you break down and explain problems.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
No description provided.