December 2023
Intermediate to advanced
504 pages
11h 43m
English
These are the solutions to the exercises found in the section Exercises.
The space complexity is O(N2). This is because the function creates the array called collection, which will end up holding N2 strings.
This implementation takes up O(N) space, as we create a new_array containing N items.
The following implementation uses this algorithm: we swap the first item with the last item in place. Then we swap the second item with the second-to-last item in place. We then proceed to swap the third item with the third-to-last item in place, and so on. Since everything is done in place and we don’t create any new data, this has a space complexity of O(1).
| | def reverse(array): |
| | i = 0 |
| | |
| | while i < len(array) // 2: |
| | mirror_of_i = ... |
Read now
Unlock full access