【问题标题】:Reducing Run Time Complexity !!! Reducing Nested loops in Python降低运行时间复杂度!!!减少 Python 中的嵌套循环
【发布时间】:2020-04-17 15:59:58
【问题描述】:

我正在开发提供/接受标准输入/输出的 python 程序。第一行给出了测试用例数T。第二行给出了数据 N 的大小。第三行和第四行分别给出长度为 N空格分隔的整数。程序安排 Set AB,其中 Set A 具有大于其同等索引的最大项目数B 中的 em> 个项目。以下是我的代码:

 def main():
   T=int(input())
   for ii in range(T):
        N=int(input())
        revo=list(map(int, input().split()))
        star=list(map(int, input().split()))
        win=0
        for i in range(N):
            a=1
            for j in range(revo[i]):
                b=revo[i]-a
                if b in star:
                    win=win+1
                    t=star.remove(b)
                    break
                a=a+1
        print(win)
main()

输入是:

1
10
3 6 7 5 3 5 6 2 9 1
2 7 0 9 3 6 0 6 2 6

输出为7,因为当优化排列时,集合 A 有 7 个项目,比集合 B 中的多。但是当我们输入大型数据集时,需要很长时间才能产生输出。有没有更好的函数可以用来减少运行时间?

【问题讨论】:

    标签: python python-3.x list algorithm loops


    【解决方案1】:

    你的解决方案是O(k*n^2),真的很多。

     def main():
       T=int(input())
       for ii in range(T): # ignoring number of inputs in time complexity
            N=int(input())
            revo=list(map(int, input().split())) # O(n), doesn't matter
            star=list(map(int, input().split())) # O(n), doesn't matter
            win=0
            for i in range(N): # n
                a=1
                for j in range(revo[i]): # n*k, k depends on 
                                         # how big is each number, hard to tell
                    b=revo[i]-a
                    if b in star: # k*n^2
                        win=win+1
                        t=star.remove(b) # normally this would multiply by n, but
                                         # remove can be called at most n times 
                        break
                    a=a+1
            print(win)
    main()
    

    如果您先对两个列表进行排序,则可以选择 O(nlogn)。这是我的解决方案:

    def solve(list1, list2):
        list1 = sorted(list1) # n*logn
        list2 = sorted(list2) # 2*n*logn
        pos_in_list1, pos_in_list2 = 0, 0
        while pos_in_list1 < len(list1): # 2*n*logn + n
            if list1[pos_in_list1] > list2[pos_in_list2]:
                pos_in_list1 += 1
                pos_in_list2 += 1
            else:
                pos_in_list1 += 1
        return pos_in_list2
    
    
    print(solve([3, 6, 7, 5, 3, 5, 6, 2, 9, 1], [2, 7, 0, 9, 3, 6, 0, 6, 2, 6]))
    # 7
    
    
    def main():
        _ = input()  # we don't need n
        list1 = [int(i) for i in input().split()] # O(n), doesn't matter
        list2 = [int(i) for i in input().split()] # O(n), doesn't matter
        print(solve(list1, list2))
    
    

    O(2*n*logn + n) = O(nlogn)

    【讨论】:

    • 这是一个很好的答案,但是,这里的时间复杂度仍然是 O(NxN),我们怎样才能达到 O(nlogn)
    • 它是 O(nlogn)。我将添加有关复杂性的 cmets
    • 你在输入中插入了逗号 ,这意味着它变成了整数,但输入实际上是一个 整数只是用空格分隔的。首先,我们必须将它们转换为整数
    • 我刚刚集成并编译了它,它在大型数据集上运行良好。谢谢
    • 可以但是还是O(n)的操作,在程序中最多执行n次,所以一共O(n^2)
    猜你喜欢
    • 2020-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 2019-03-17
    • 1970-01-01
    相关资源
    最近更新 更多