发布于2025-01-05 09:03 阅读(762) 评论(0) 点赞(4) 收藏(3)
Given a python list that contains nested lists of arbitrary levels of nesting, the goal is to return a completely flattened list i.e for the sample input [1, [2], [[[3]]], 1]
, the output should be [1, 2, 3, 1]
.
My solution:
def flatten(lst):
stack = [[lst, 0]]
result = []
while stack:
current_lst, start_index = stack[-1]
for i in range(start_index, len(current_lst)):
if isinstance(current_lst[i], list):
# Update the start_index of current list
# to the next element after the nested list
stack[-1][1] = i + 1
# Add nested list to stack
# and initialize its start_index to 0
stack.append([current_lst[i], 0])
# Pause current_lst traversal
break
# non list item
# add item to result
result.append(current_lst[i])
else:
# no nested list
# remove current list from stack
stack.pop()
return result
I would like to know the time and space complexity (auxiliary space) of my solution if correct.
What I think
Time Complexity:
I believe the solution has a time complexity of O(m + n) where m is the total number of nested lists at all levels and n is the total number of atomic elements at all levels (non list elements)
Space Complexity:
I believe the space complexity is O(d), where d is the depth of the most nested list. This is because the stack tracks the current state of traversal, and its size is proportional to the nesting depth
Is the solution correct?
Is the time and space analysis correct?
Yes, the solution is correct.
Yes, the time and space analysis are correct ... if you don't count the space used by result
as auxiliary space, which is reasonable. Although note that result
overallocates/reallocates, which you could regard as taking O(n) auxiliary space. You could optimize that by doing two passes over the whole input, one to count the atomic elements, then create result = [None] * n
, and then another pass to fill it.
Btw it's better to use iterators instead of your own list+index pairs (Attempt This Online!):
def flatten(lst):
stack = [iter(lst)]
result = []
while stack:
for item in stack[-1]:
if isinstance(item, list):
stack.append(iter(item))
break
result.append(item)
else:
stack.pop()
return result
Or with the mentioned space optimization (Attempt This Online!):
def flatten(lst):
def atoms():
stack = [iter(lst)]
while stack:
for item in stack[-1]:
if isinstance(item, list):
stack.append(iter(item))
break
yield item
else:
stack.pop()
n = sum(1 for _ in atoms())
result = [None] * n
i = 0
for result[i] in atoms():
i += 1
return result
作者:黑洞官方问答小能手
链接:https://www.pythonheidong.com/blog/article/2046776/385e9e96111b0ef3ab92/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!