Skip to content

Conversation

@wilson-romero
Copy link

No description provided.

@0xp4blo 0xp4blo changed the title [solution] challenge python 9 Submission - wilson-romero May 22, 2020
Copy link
Contributor

@0xp4blo 0xp4blo left a 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

while True:
try:
next_index = arr.index(0, next_index)
arr.insert(next_index + 1, 0)
Copy link
Contributor

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.

@0xp4blo
Copy link
Contributor

0xp4blo commented May 24, 2020

Think about what happens to the array when you do

arr[step + 1:] = arr[step:-1]

What happens what the input array is [0, 0, ..., 0]?

Comment on lines 11 to 14
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)
Copy link
Contributor

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.

Comment on lines 18 to 19
for step, value in enumerate(zeros):
arr[value + 1:] = arr[value:-1]
Copy link
Contributor

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).

@0xp4blo 0xp4blo added the submission Solution to problem label May 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

submission Solution to problem

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants