-
-
Notifications
You must be signed in to change notification settings - Fork 29
Manchester | 25-SDC-Nov | Rahwa Haile | Sprint 2 | improve_with_precomputing #128
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 2 commits
f591dda
345a01a
c062739
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 |
|---|---|---|
|
|
@@ -3,3 +3,23 @@ Each directory contains implementations that can be made faster by using pre-com | |
| The problem they solve is described in docstrings and tests. | ||
|
|
||
| Speed up the solutions by introducing precomputing. You may rewrite as much of the solution as you want, and may add any tests, but must not modify existing tests. | ||
|
|
||
| ## Improvements Made | ||
|
|
||
| ### common_prefix | ||
|
|
||
| **Original Approach (Nested Loops):** | ||
| - Time Complexity: **O(n² × m)** - Compares every string with every other string (n²) pairs, each taking O(m) comparisons | ||
|
|
||
| **Optimized Approach (Sort + Adjacent Pairs):** | ||
| - Time Complexity: **O(n log n + n × m)** - Sort once (n log n), then compare only adjacent pairs (n comparisons) | ||
| - **Why better:** The longest common prefix will be found in the first and last strings after sorting, eliminating unnecessary comparisons. Reduces from O(n²) to O(n) pair comparisons. | ||
|
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. What do you mean by first and last strings? That sounds like only two comparisons are needed to find the longest prefix after sorting.
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. Thanks @cjyuan for the detailed feedback! You're absolutely right on both points. I've updated the README to correct these issues |
||
|
|
||
| ### count_letters | ||
|
|
||
| **Original Approach (Repeated String Checks):** | ||
| - Time Complexity: **O(n²)** - For each letter, checking if lowercase version exists in string is O(n), done n times | ||
|
|
||
| **Optimized Approach (Precomputed Set):** | ||
| - Time Complexity: **O(n)** - Precompute lowercase letters into a set (O(n)), then check membership is O(1) each time | ||
| - **Why better:** Replaces repeated O(n) string searches with single O(n) precomputation and O(1) set lookups, reducing overall from O(n²) to O(n). | ||
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.
If we are factoring in the length of the strings, m, then the complexity of sorting won't just be O(nlogn). nlogn measures only the number of comparisons needed.