【问题标题】:What is the Big O Notation of this function?这个函数的大 O 表示法是什么?
【发布时间】:2019-03-22 20:25:12
【问题描述】:
def scramble(s1, s2):
    arrs1 = list(s1)
    arrs2 = list(s2)

    if all(True if arrs2.count(item) <= arrs1.count(item) else False for 
item in arrs2):
        return True
    else:
        return False

我正在尝试创建一个函数,该函数可以测试字符串字符 (str1) 的一部分是否可以重新排列以匹配另一个字符串 (str2)。

这不是 O(n) 吗?

【问题讨论】:

  • 不,你在循环中调用.count,它是O(n),因此它是O(N^2)

标签: python python-3.x algorithm optimization big-o


【解决方案1】:

如果您想要平均时间复杂度为 O(n) 的解决方案,您可以改用 collections.Counter

from collections import Counter
def s(s1, s2):
    c1, c2 = Counter(s1), Counter(s2)
    return all(v <= c1.get(k, 0) for k, v in c2.items())

【讨论】:

    【解决方案2】:

    为了扩展 Ajax 的答案,没有 all 语句的代码看起来像这样:

    def scramble(s1, s2):
      arrs1 = list(s1)
      arrs2 = list(s2)
    
      are_counts_lte = []
      for item in arrs2:
        # this next "count" statement makes it O(n^2)
        if arrs2.count(item) <= arrs1.count(item):
          are_counts_lte.append(True)
        else:
          are_counts_lte.append(False)
    
      for b in are_counts_lte:
        if b is False: return False
      return True
    

    这显然是 O(n^2)

    【讨论】:

      【解决方案3】:

      贴出的代码其实是O(n^2)

      all(True if arrs2.count(item) <= arrs1.count(item) else False for item in arrs2)
      

      all 需要对输入进行单次传递,产生 O(n) 的时间复杂度。但是,在每次传递中,必须获得itemarrs2arrs1 中出现的次数。 count 的复杂性是 O(n),因为列表对象也必须迭代才能找到所需值的每次出现。 count 方法被调用了两次,但是,它仍然接近 O(n) 作为平均时间复杂度。因此,完整的表达式为O(n)*O(n) =&gt; O(n^2)

      【讨论】:

      • 非常感谢,这很有帮助。
      猜你喜欢
      • 1970-01-01
      • 2015-04-24
      • 1970-01-01
      • 2021-03-04
      • 2021-12-28
      • 2018-07-16
      • 2011-04-13
      • 2017-12-07
      • 2015-05-23
      相关资源
      最近更新 更多