-
Notifications
You must be signed in to change notification settings - Fork 18
Submission - wilson-romero #10
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?
Conversation
0xp4blo
left a comment
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.
Solution isn't optimum
solutions/wilson_romero/solution.py
Outdated
| while True: | ||
| try: | ||
| next_index = arr.index(0, next_index) | ||
| arr.insert(next_index + 1, 0) |
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.
Watch out with this operation. Your solution isn't optimum, its O(n^2) in time and O(n) in space. There's a O(n) in time and O(1) in space solution.
|
Think about what happens to the array when you do arr[step + 1:] = arr[step:-1]What happens what the input array is |
| zeros = [] | ||
| for step, value in enumerate(arr): | ||
| if value == 0 and step >= index: | ||
| arr[step + 1:] = arr[step:-1] | ||
| index = step + 2 | ||
| if value == 0: | ||
| zeros.append(step) |
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 creating additional space, i.e.: for an array full of 0s of length n, you'll be creating an auxiliary array of size n containing its indices.
solutions/wilson_romero/solution.py
Outdated
| for step, value in enumerate(zeros): | ||
| arr[value + 1:] = arr[value:-1] |
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 operation performed with slices is highly inefficient. This for loop itself have time complexity O(n^2).
No description provided.