【问题标题】:How to code this in O(n) time instead of O(n^2)?如何在 O(n) 时间而不是 O(n^2) 内对此进行编码?
【发布时间】:2022-12-03 10:11:06
【问题描述】:

我有 3 个列表:

在逗号之间添加空格,以便更好地理解我将使用的 groupby。

a=[1,1,1,1    ,2,2,2    ,3,3,3,3,3,3,    4,4]
b=['l2','l5','l1','l1'    ,'l5','l2',''    , 'l1','l3','l6','l2','l5','l1'  , 'l5','l1']
c=['z','z','a','s'     ,'z','z','a',    's','z','z','a','s','d'  , 's','' ]

在列表“a”中,我有一些组,根据这些组,我想在列表“c”中相对于列表“b”进行更改。

列表 'a' 有 1 组,所以对于那些 1 的索引,我正在检查列表 'b' 并且无论我在哪里找到字符串 'l5',我都想让该组的所有进一步索引在列表中为空字符串( '' ) 'C'。

例如:

If list 'a' has [1,1,1,1] and list b has ['l2','l5','l1','11']. 我想在 'l5' 之后创建索引,即列表 c 中的索引 2 和 3 为空。 列表 c 的预期输出为:

c= ['z','z','','']

我为此编写了一个代码,它工作得很好,但我认为这在 O(n^2) 的时间复杂度内有效。有什么方法可以优化此代码并使其在 O(n) 时间内运行以使其更快?

这是我的代码:

a=[1,1,1,1    ,2,2,2    ,3,3,3,3,3,3,    4,4]
b=['l2','l5','l1','l1'    ,'l5','l2',''    , 'l1','l3','l6','l2','l5','l1'  , 'l5','l1']
c=['z','z','a','s'     ,'z','z','a',    's','z','z','a','s','d'  , 's','' ]

from itertools import groupby
g= groupby(a) 
m=0           
for group,data in g:
    n = len(list(data))   #length of each group
    m+=n                  #this stores the sum of length of groups (To get the last index of each group)
    while m:
        h=m-n     #to get the beginning index of each group(total-length_of_currentgroup)
        nexxt=0
        for i in range(h,m):  #this loops for each group (where h is start and m is ending index of each group)
            if b[i]=='l5' and nexxt==0:
                nexxt=i+1
        while nexxt<m and nexxt!=0:
            c[nexxt] = ''
            nexxt+=1
        break
print(c)

输出:

['z', 'z', '', '', 'z', '', '', 's', 'z', 'z', 'a', 's', '', 's', '']

有没有办法在 O(N) 时间内写这个?

【问题讨论】:

    标签: arrays algorithm data-structures stack time-complexity


    【解决方案1】:

    是的,可以优化此代码并使其在 O(n) 时间内运行。这样做的关键是避免使用嵌套循环,随着输入大小的增加,嵌套循环可能会很耗时。

    优化此代码的一种方法是使用字典来存储组号和字符串“l5”在相应组中最后一次出现的索引。然后,我们可以遍历列表 c 一次,并检查当前索引是否大于相应组中最后一次出现的 'l5' 的存储索引。如果是,我们可以将 c 中的当前元素设置为空字符串。

    这是一个如何工作的示例:

    # Create a dictionary to store the group number and the index of the last occurrence of 'l5'
    last_index = {}
    
    # Iterate over the groups in 'a'
    for group, data in groupby(a):
      # Get the length of the current group
      n = len(list(data))
      
      # Iterate over the elements in 'b' for the current group
      for i in range(m, m + n):
        # If the current element in 'b' is 'l5', update the last_index dictionary
        if b[i] == 'l5':
          last_index[group] = i
      
      # Increment the current index
      m += n
    
    # Iterate over the elements in 'c'
    for i in range(len(c)):
      # If the current index is greater than the stored index of the last occurrence of 'l5' in the corresponding group, set the element to an empty string
      if i > last_index.get(a[i], 0):
        c[i] = ''
    
    # Print the updated list 'c'
    print(c)
    

    这段代码应该在 O(n) 时间内运行,因为我们只对列表 a 和 b 各迭代一次,并且我们只对列表 c 迭代一次。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-23
      • 2015-06-29
      • 1970-01-01
      • 1970-01-01
      • 2021-08-13
      • 1970-01-01
      • 2017-08-13
      • 1970-01-01
      相关资源
      最近更新 更多