-
Notifications
You must be signed in to change notification settings - Fork 14
Solution - #1 - Nishitha - 10/05/2025 #49
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
base: master
Are you sure you want to change the base?
Solution - #1 - Nishitha - 10/05/2025 #49
Conversation
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.
this is a O(NlogN) TC and s O(1) SC , we can have a solution with a better time complexity
You can add multiple solutions, as well as their explanations. The idea is to start from a not so optimized solution, and gradually move to an optimized solution. You can follow this for reference: https://github.com/Dijkstra-Edu/LeetCode-Solutions/pull/3/files |
class Solution { | ||
public: | ||
bool containsDuplicate(vector<int>& nums) { | ||
sort(nums.begin(), nums.end()); // O(n log n) | ||
for (int i = 1; i < nums.size(); ++i) { | ||
if (nums[i] == nums[i - 1]) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
}; | ||
|
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.
- The File name is not following convention.
- There is no explanation.md file. Kindly read it and have a clear understanding on how to proceed. You can use the template file: https://github.com/Dijkstra-Edu/LeetCode-Solutions/blob/master/Explanation-Template.md
- Also use these merged PR's for reference: https://github.com/Dijkstra-Edu/LeetCode-Solutions/pulls?q=is%3Apr+is%3Aclosed
class Solution { | ||
public: | ||
bool containsDuplicate(vector<int>& nums) { | ||
sort(nums.begin(), nums.end()); // O(n log n) | ||
for (int i = 1; i < nums.size(); ++i) { | ||
if (nums[i] == nums[i - 1]) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
}; |
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.
Same as the solution above. And not following convention. It should be 1 Solution at a time. Follow the Folder structure.
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.
Changes need to be made. Please read the readme.md clearly.
Also the title is still not clear. Kindly have a look at the readme. |
Problem Explanation
Problem Statement
Given an integer array
nums
, returntrue
if any value appears at least twice in the array, and returnfalse
if every element is distinct.Approach
Code Explanation
sort(nums.begin(), nums.end());
: Sorts the array in ascending order.for (int i = 1; i < nums.size(); ++i)
: Iterates through the array starting from the second element.if (nums[i] == nums[i - 1])
: Checks if the current element is equal to the previous one.return true;
: Returns true immediately upon finding a duplicate.return false;
: If no duplicates are found after the loop, returns false.