Repository navigation
NW| 26-Jul-SDC | Ahmad Hmedan | Sprint 2 | Improve code with precomputing - #236
AhmadHmedann wants to merge 4 commits into
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
| seen = set() | ||
| longest = "" | ||
| for string_index, string in enumerate(strings): | ||
| for other_string in strings[string_index+1:]: | ||
| common = find_common_prefix(string, other_string) | ||
| if len(common) > len(longest): | ||
| longest = common | ||
| return longest | ||
|
|
||
| for string in strings: | ||
| for i in range(1, len(string) + 1): | ||
| prefix = string[:i] | ||
| if prefix in seen and len(prefix) > len(longest): | ||
| longest = prefix |
There was a problem hiding this comment.
Can you use complexity to explain how the new implementation is better than the original implementation?
There was a problem hiding this comment.
Original:
- Time Complexity: O(N² × K)
- Space Complexity: O(N + K)
New:
- Time Complexity: O(N × K²)
- Space Complexity: O(N × K)
Where:
- N is the number of strings in the list.
- K is the maximum length of a string.
There was a problem hiding this comment.
The time complexity analyses are correct.
Both the original and the refactored code require more space. If string length,
There was a problem hiding this comment.
Do you mean the input space complexity?
I was calculating the auxiliary space complexity, excluding the input memory. Should I make that clear in my analysis?
I also realised that because of prefix = string[:i] and storing all the prefixes in a set, the auxiliary space complexity of the new implementation should be O(N × K²), not O(N × K).
Is my understanding correct?
There was a problem hiding this comment.
Do you mean the input space complexity?
Yes
Should I make that clear in my analysis?
No need. Since your space complexity analyses were wrong. I was just trying to clarify.
I also realised that because of
prefix = string[:i]and storing all the prefixes in a set, the auxiliary space complexity of the new implementation should be O(N × K²), not O(N × K).Is my understanding correct?
Correct.
Learners, PR Template
Self checklist
Task code
CYF-1183
Changelist
Speed up the solutions by introducing precomputing