Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
18 changes: 18 additions & 0 deletions solutions/wilson_romero/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
""" Module solution """
from typing import List

class Solution:
""" Challenge solution """
def duplicate_zeros(self, arr: List[int]):
"""Duplicate zeros and shift right"""
next_index = 0
length = len(arr)
while True:
try:
next_index = arr.index(0, next_index)
arr.insert(next_index + 1, 0)
Copy link
Copy Markdown
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.

arr.pop()
next_index = next_index + 2
except ValueError:
break
arr = arr[:length]