-
-
Notifications
You must be signed in to change notification settings - Fork 25
West Midlands | 25-SDC-July | Gabriel Deng | Sprint 1 | Analyse and refactor functions #69
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: main
Are you sure you want to change the base?
Changes from all commits
b3206ba
f6d1d33
846c64f
3938915
05e5a49
9db3262
4ad8e18
eade984
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,23 @@ | ||
| /** | ||
| * Find if there is a pair of numbers that sum to a given target value. | ||
| * | ||
| * Time Complexity: | ||
| * Space Complexity: | ||
| * Optimal Time Complexity: | ||
| * Time Complexity: O(n) | ||
| * Space Complexity: O(1) | ||
| * Optimal Time Complexity: O(n) | ||
| * | ||
|
Comment on lines
4
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. Are these the complexities of the original function, the improved function, or something else? An O(1) approach? How?
Author
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. I mixed the complexities around and they are for this improved function, Time complexity is O(n) as you have to scan through the whole array |
||
| * @param {Array<number>} numbers - Array of numbers to search through | ||
| * @param {number} target - Target sum to find | ||
| * @returns {boolean} True if pair exists, false otherwise | ||
| */ | ||
|
|
||
| // The time complexity in the new implementation is O(n) because we only loop and iterated through the array once. The use of set to add and check value for occurrence is O(1) which is the best Optimal time complexity | ||
|
|
||
| export function hasPairWithSum(numbers, target) { | ||
| for (let i = 0; i < numbers.length; i++) { | ||
| for (let j = i + 1; j < numbers.length; j++) { | ||
| if (numbers[i] + numbers[j] === target) { | ||
| return true; | ||
| } | ||
| if (numbers[i] + numbers[i + 1] === target) { | ||
| 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.
What is the complexity of the improved approach?
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 complexity for this improved approach is O(n + m) compared to the original O(n * m)